具有多种字体颜色的UILabel文本

aah*_*ens 3 iphone objective-c textcolor uilabel ios

有没有办法让两个不同的textColor财产?基本上我试图让在第一个字符是,其余的是.我想避免使用两种不同的因为我可能想要更改绿色字符文本中的位置.UILabelUIColorsUILabel text propertygreenColorblackColorUILabels

ios*_*ist 7

从ios 6开始,您可以执行以下操作:

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]  initWithString:string];
[attributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,3)];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(4,9)];
cell.label.attributedText = attributedString;
Run Code Online (Sandbox Code Playgroud)