【发布时间】:2011-11-01 17:41:03
【问题描述】:
我必须在标签的每个单词周围创建一个圆角矩形,如下所示:
我可以使用stretchableImageWithLeftCapWidth:topCapHeight: 之类的方法还是可以将其设置为背景视图?
非常感谢!
【问题讨论】:
标签: iphone view background label
我必须在标签的每个单词周围创建一个圆角矩形,如下所示:
我可以使用stretchableImageWithLeftCapWidth:topCapHeight: 之类的方法还是可以将其设置为背景视图?
非常感谢!
【问题讨论】:
标签: iphone view background label
您不能轻松地逐字逐句地做到这一点。我会创建一个父视图,然后将文本拆分为单词并为每个单词添加一个标签。然后,您的可拉伸图像方法将适用于单个标签。
【讨论】:
拉伸图像会导致呈现丑陋的效果,特别是如果您的字词很长。
我会这样处理:
创建此层次结构:
- UIView theView
-- UIView imagesView
--- UIImageView startBackgroundImage
--- UIImageView endBackgroundImage
-- UILabel theLabel
theView 将是包含您的标签和背景图像的视图,theLabel 将包含标签本身,而 imagesView 包含您需要的所有图像视图。
所以首先我们将设置标签的文本并获取字符串的大小:
theLabel.text = @"Food";
theLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"content.png"]];
CGSize textSize = [[theLabel text] sizeWithFont:[theLabel font]];
最后,我们将设置view的大小(imagesView将具有相同的大小)以及startBackgroundImage和endBackgroundImage的位置。
int height = startBackgroundImage.image.size.height;
int startBackgroundImageWidth = startBackgroundImage.image.size.width;
int endBackgroundImageWidth = endBackgroundImage.image.size.width;
theView.frame = CGRectMake(xYouWant,yYoutWant,textSize.width+startBackgroundImageWidth+endBackgroundImageWidth,height);
startBackgroundImage.frame = CGRectMake(0,0,startBackgroundImage.frame.width,startBackgroundImage.frame.height);
endBackgroundImage.frame = CGRectMake(textSize.width+endBackgroundImageWidth,0,endBackgroundImage.frame.width,endBackgroundImage.frame.height);
无法上传图片,所以我将尝试描述:
startBackgroundImage 将包含图像的左侧(四舍五入)
endBackgroundImage 将包含图像的右侧(四舍五入)
content.png 将包含一个几像素宽的图像,将用作图案。
【讨论】: