NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:string];
//设置颜色
[str addAttribute:NSForegroundColorAttributeName
value:[UIColor blueColor]
range:NSMakeRange(0,5)];
[str addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(6,12)];
[str addAttribute:NSForegroundColorAttributeName
value:[UIColor greenColor]
range:NSMakeRange(19,6)];
//设置字体
[str addAttribute:NSFontAttributeName
value:[UIFont fontWithName:@"Arial" size:30.0]
range:NSMakeRange(0, 5)];
[str addAttribute:NSFontAttributeName
value:[UIFont fontWithName:@"Arial" size:30.0]
range:NSMakeRange(6, 12)];
[str addAttribute:NSFontAttributeName
value:[UIFont fontWithName:@"Arial" size:30.0]
range:NSMakeRange(19, 6)];
label.attributedText = str;
本文展示了一个使用 NSMutableAttributedString 的示例,演示了如何为不同部分的文字设置不同的颜色和字体大小。
1793

被折叠的 条评论
为什么被折叠?



