如何仅将背景颜色设置为UILabel文本

时间:2016-07-07 15:39:36

标签: ios objective-c ipad uilabel

我在应用程序中工作,我需要设置标签,如附加图像。如果有人有任何想法,请告诉我?enter image description here

8 个答案:

答案 0 :(得分:4)

你可以这样做

NSString *yourString1=@"What Does your friends really";
NSString *yourString2=@"Think of your spouce?";

NSString *str1=[NSString stringWithFormat:@"  %@..\n",yourString1];
NSString *str2=[NSString stringWithFormat:@"  %@,,",yourString2];
NSString *str3=[NSString stringWithFormat:@"%@%@",str1,str2];    

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:[str1 stringByAppendingString:str2]];
NSRange range = [str1 rangeOfString:@".."];
NSRange range1 = [str3 rangeOfString:@",,"];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:range];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:range1];    
[attributedString addAttribute: NSBackgroundColorAttributeName value: [UIColor orangeColor] range: NSMakeRange(0, str1.length)];
[attributedString addAttribute: NSBackgroundColorAttributeName value: [UIColor redColor] range: NSMakeRange(str1.length, str2.length)];

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:5];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [str2 length])];

lblTest.attributedText = attributedString;

本守则的输出:

enter image description here

答案 1 :(得分:3)

如果在字符串上使用带有背景颜色的属性字符串不起作用,那么您可能需要创建2个单独的标签,并在它们之间留出空格并在每个标签上设置背景颜色。

答案 2 :(得分:3)

您可能需要一些具有文本结束背景颜色的空间。我用自己的技巧解决了这个问题。添加一些逗号或指向分隔字符串。然后将其应用于字符串。无需创建额外的标签。

  NSArray *aArray = [@" Font Size  .k" componentsSeparatedByString:@".k"];

NSMutableAttributedString *fulltext=[[NSMutableAttributedString alloc] initWithString:@""];

NSMutableAttributedString *title1=[[NSMutableAttributedString alloc] initWithString:[aArray objectAtIndex:0]];
NSMutableAttributedString *title2=[[NSMutableAttributedString alloc] initWithString:[aArray objectAtIndex:1]];

//[title1 addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:255.0/255.0 green:84.0/255.0 blue:49.0/255.0 alpha:1.0] range:NSMakeRange(0,title1.length)];
[title1 addAttribute:NSBackgroundColorAttributeName
               value:[UIColor colorWithRed:255.0/255.0 green:84.0/255.0 blue:49.0/255.0 alpha:1.0]
               range:NSMakeRange(0, title1.length)];
[title1 addAttribute:NSFontAttributeName
               value:[UIFont boldSystemFontOfSize:(isIpad||isIPadPro)?19.0f:16.0f]
               range:NSMakeRange(0,title1.length)];

[title2 addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:NSMakeRange(0,title2.length)];
[title2 addAttribute:NSBackgroundColorAttributeName
               value:[UIColor clearColor]
               range:NSMakeRange(0, title2.length)];
[title2 addAttribute:NSFontAttributeName
               value:[UIFont boldSystemFontOfSize:(isIpad||isIPadPro)?19.0f:16.0f]
               range:NSMakeRange(0,title2.length)];

[fulltext appendAttributedString:title1];
[fulltext appendAttributedString:title2];

self.textLabel.attributedText = fulltext;

我的输出如下:

enter image description here

现在让K的背景(标题2)清晰,这样你就可以在文字结束后获得空格!

答案 3 :(得分:2)

//setting dummy text to label
self.lbLog.text=@"This is Simple Text With Red background Color";

//creating attributed string 
NSMutableAttributedString *attribString =
[[NSMutableAttributedString alloc] initWithString:self.lbLog.text];

//setting background color to attributed text
[attribString addAttribute:NSBackgroundColorAttributeName
          value:[UIColor redColor]
          range:NSMakeRange(0, attribString.length)];

//setting attributed text to label
self.lbLog.attributedText = attribString;

答案 4 :(得分:2)

lblText.backgroundColor=UIColor.red

答案 5 :(得分:0)

如果所有标签文本的背景颜色对于标签中的所有文本都相同,则需要使用单独的标签设置标签的背景颜色,否则用于不同的背景颜色用于您需要的某些文本使用 NSMutableAttributedString

label.backgroundColor = [UIColor colorWithRed:29.0/255.0 green:135.0/255.0 blue:145.0/255.0 alpha:1.0];

答案 6 :(得分:0)

使用两个自定义标签(或代替标签使用uiview作为背景),一个用于您的背景,将backgroundcolor设置为清晰颜色,另一个标签用于您的文本设置backgroundcolor作为您想要的颜色。

答案 7 :(得分:0)

我们可以在UItextview的帮助下完成这项工作。我做到了,我会很快发布代码。