在标签文本的背景颜色

时间:2017-10-03 10:59:17

标签: ios objective-c xcode uilabel background-color

大家好我需要在iOS上做这样的事情。我试图在标签中突出显示文字,但结果不正确。客观C中的一些想法?enter image description here

我需要在uitableviewcell中执行此操作,因此我必须以编程方式执行此操作,而不是在故事板中执行此操作。文字应该改变,分为1行,2行,3行

2 个答案:

答案 0 :(得分:2)

使用文本背景颜色设置属性字符串。

UILabel *label = [[UILabel alloc] init];
NSString * textStr = @"This is test label text with blue background color";

//Attributed text
NSMutableAttributedString *attrbStr = [[NSMutableAttributedString alloc] initWithString: textStr];

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

label.attributedText = attrbStr;

答案 1 :(得分:-1)

UILabel* label = [[UILabel alloc] init];
label.backgroundColor = [UIColor darkGrayColor];
相关问题