我想用2种颜色创建一个uilabel。第一种颜色是黑色,另一种颜色是蓝色。
我可以使用多个uilabel,但我想只有一个uilabel。有什么办法可以实现吗?
这应该是输出。
这是我的代码:
UILabel * lblPostContent = [[UILabel alloc] initWithFrame:CGRectMake((ICON_PADDING*1.5), 42, container.frame.size.width-30, 34)];
lblPostContent.numberOfLines =0;
[lblPostContent setFont:[UIFont systemFontOfSize:11]];
[lblPostContent setText:[NSString stringWithFormat:@"I just scored %d points at %@ using the iBowl app", score, LuckyStrikes]];
[container addSubview:lblPostContent];
答案 0 :(得分:10)
你应该使用属性......像这样
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:yourString];
[text addAttribute: NSForegroundColorAttributeName value: [UIColor blackColor] range: NSMakeRange(0, TXTTOBEBLACKLENGTH)];
[text addAttribute: NSForegroundColorAttributeName value: [UIColor blueColor] range: NSMakeRange(TXTTOBEBLACKLENGTH, TXTTOBEBLBLUELENGTH)];
[lblPostContent setAttributedText: text];
答案 1 :(得分:5)
为避免必须计算文本部分的长度并生成NSRange
,您可以使用NSMutableAttributedString
从NSAttributedString
组件组成最终的appendAttributedString:
,如下所示:< / p>
UIColor *normalColor = [UIColor blackColor];
UIColor *highlightColor = [UIColor blueColor];
UIFont *font = [UIFont systemFontOfSize:12.0];
NSDictionary *normalAttributes = @{NSFontAttributeName:font, NSForegroundColorAttributeName:normalColor};
NSDictionary *highlightAttributes = @{NSFontAttributeName:font, NSForegroundColorAttributeName:highlightColor};
NSAttributedString *normalText = [[NSAttributedString alloc] initWithString:@"Normal " attributes:normalAttributes];
NSAttributedString *highlightedText = [[NSAttributedString alloc] initWithString:@"Highlighted" attributes:highlightAttributes];
NSMutableAttributedString *finalAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:normalText];
[finalAttributedString appendAttributedString:highlightedText];
self.label.attributedText = finalAttributedString;
答案 2 :(得分:0)
这是AttributedString的扩展函数:
extension NSMutableAttributedString {
func setColorForText(textToFind: String, withColor color: UIColor) {
let range: NSRange = self.mutableString.range(of: textToFind, options: .caseInsensitive)
self.addAttribute(NSAttributedStringKey.foregroundColor, value: color, range: range)
}
}
使用Label:
尝试let label = UILabel()
label.frame = CGRect(x: 40, y: 100, width: 280, height: 200)
let first = "89"
let second = "Lucky Strikes"
let third = "iBowl app"
let stringValue = "I just scored \(first) points at \(second) using the \(third)" // or direct assign single string value like "firstsecondthird"
label.textColor = UIColor.lightGray
label.numberOfLines = 0
let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: stringValue)
attributedString.setColorForText(textToFind: first, withColor: UIColor.blue) // use variable for string "89"
attributedString.setColorForText(textToFind: second, withColor: UIColor.blue) // or direct string like this "Lucky Strikes"
attributedString.setColorForText(textToFind: third, withColor: UIColor.blue)
label.font = UIFont.systemFont(ofSize: 26)
label.attributedText = attributedString
self.view.addSubview(label)
结果如下: