Iphone 围绕每个单词的圆角矩形

Iphone 围绕每个单词的圆角矩形,iphone,view,background,label,Iphone,View,Background,Label,我必须在标签的每个单词周围创建一个圆角矩形,如下所示: 我可以使用像StretchableMagewithLeftCapWidth:topCapHeight:这样的方法,还是可以将其设置为背景视图 非常感谢你 你不可能一字不差地做到这一点。我将创建一个父视图,然后将文本拆分为单词,并为每个单词添加一个标签。您的可拉伸图像方法将适用于单个标签。拉伸图像会导致渲染效果不佳,特别是如果您有很长的单词。 我想这样做: 创建此层次结构: - UIView theView -- UIView imagesV

我必须在标签的每个单词周围创建一个圆角矩形,如下所示: 我可以使用像StretchableMagewithLeftCapWidth:topCapHeight:这样的方法,还是可以将其设置为背景视图


非常感谢你

你不可能一字不差地做到这一点。我将创建一个父视图,然后将文本拆分为单词,并为每个单词添加一个标签。您的可拉伸图像方法将适用于单个标签。

拉伸图像会导致渲染效果不佳,特别是如果您有很长的单词。 我想这样做:

创建此层次结构:

- UIView theView
-- UIView imagesView
--- UIImageView startBackgroundImage
--- UIImageView endBackgroundImage
-- UILabel theLabel
视图将是包含标签和背景图像的视图,标签将包含标签本身,图像视图包含您需要的所有图像视图

因此,首先我们将设置标签的文本并获得字符串的大小:

theLabel.text = @"Food";
theLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"content.png"]];
CGSize textSize = [[theLabel text] sizeWithFont:[theLabel font]];
最后,我们将设置视图图像的大小。视图的大小和位置将与开始背景图像和结束背景图像相同

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将包含一个像素宽的图像,该图像将用作图案。

是的,我的字符串只有一个单词。。。我试过这个代码,但不起作用。。。如果在创建图像时考虑了拉伸,并且正确定义了封盖,拉伸图像不会产生难看的结果-这就是iPhone上大多数按钮的创建方式。在图像的中心有一个1px的部分,可以在拉伸的长度上重复。是的,还有一个更灵活的版本ResizebleimageWithCapinsets:。