开发中,我们经常会遇到一行字,但是显示不同颜色和字体的情况,话不多说,直接上代码。
1、显示不同颜色,有两种方式
(1)通过 range 来设置
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"点击代表您已阅读并同意用户规则和协议"];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0,11)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(11,4)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(16,2)];