【问题标题】:rounded rectangle around every word iPhone每个单词周围的圆角矩形 iPhone
【发布时间】:2011-11-01 17:41:03
【问题描述】:

我必须在标签的每个单词周围创建一个圆角矩形,如下所示:

我可以使用stretchableImageWithLeftCapWidth:topCapHeight: 之类的方法还是可以将其设置为背景视图?

非常感谢!

【问题讨论】:

    标签: iphone view background label


    【解决方案1】:

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

    【讨论】:

    • 是的,我的字符串只有一个字...我试过这段代码,但它不起作用...pastebin.com/UaCUPd71
    【解决方案2】:

    拉伸图像会导致呈现丑陋的效果,特别是如果您的字词很长。
    我会这样处理:

    创建此层次结构:

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

    【讨论】:

    • 如果在创建图像时考虑了拉伸并且正确定义了上限,则拉伸图像不会产生难看的结果——这就是 iPhone 上大多数按钮的创建方式。图像中心有一个 1px 的部分,可以重复拉伸的长度。
    • 是的,还有一个更灵活的版本 (resizableImageWithCapInsets:)。
    猜你喜欢
    • 1970-01-01
    • 2017-06-24
    • 2011-03-22
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-07
    相关资源
    最近更新 更多