如何在同一标签中使用不同的颜色

Md.*_*ury 2 iphone objective-c uilabel

我要编写它的问题已经解决了使用两个UILabel.我正在搜索一些优化方式.我有一个(example : hello world )需要UILabel用不同颜色显示的字符串"hello in red color " and "world in green color"

提前致谢

Ram*_*uri 5

假设您有一个名为coloredLabel的文本标签,并且您希望以红色和绿色世界编写hello.
在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;
Run Code Online (Sandbox Code Playgroud)