我要写的问题已经使用两个UILabel
解决了。我正在寻找一些优化方法。
我有一个字符串 (示例:hello world )
需要在具有不同颜色的 UILabel
中显示。例如 “红色的 hello ”和“绿色的世界”颜色”
提前致谢
最佳答案
假设您有一个名为 ColoredLabel 的文本标签,并且您想用红色写“hello”,用绿色写“world”。
从 iOS 6 开始,您可以创建属性字符串并将其设置为标签的属性文本:
NSMutableAttributedString* str=[[NSMutableAttributedString alloc]initWithString: @"Hello world"];
[str setAttributes: @{ NSForegroundColorAttributeName : [UIColor redColor] } range: NSMakeRange(0, 5) ];
[str setAttributes: @{ NSForegroundColorAttributeName : [UIColor greenColor] } range: NSMakeRange(6, 5) ];
coloredLabel.attributedText= str;
关于iphone - 如何在同一标签上使用不同颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13452662/