我想在 UILabel
中显示这样的字符串:
There are 5 results.
其中数字 5 为红色,字符串的其余部分为黑色。
如何在代码中做到这一点?
最佳答案
这样做的方法是像这样使用 NSAttributedString
:
NSMutableAttributedString *text =
[[NSMutableAttributedString alloc]
initWithAttributedString: label.attributedText];
[text addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(10, 1)];
[label setAttributedText: text];
我创建了一个 UILabel
extension to do it .
关于ios - 带有两种不同颜色文本的 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6501808/