富文本可以将一串文字上的内容根据需求设置成不同的字体或大小如:“我是中国人”,我们将“我是中国人”中的“中国”设置成红色,而将“人”设置成较大的字体。
代码:
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"更多细则,请参见《会员礼遇条款》"];
[AttributedStr addAttribute:NSFontAttributeName
value:Font12
range:NSMakeRange(9, 6)];
[AttributedStr addAttribute:NSForegroundColorAttributeName
value:color
range:NSMakeRange(9, 6)];
[AttributedStr addAttribute:NSForegroundColorAttributeName
value:Color153
range:NSMakeRange(0, 9)];
[AttributedStr addAttribute:NSForegroundColorAttributeName
value:Color153
range:NSMakeRange(15, 1)];
aboutBeniLbl.attributedText = AttributedStr
上述代码就将“会员礼遇条款”的颜色和大小做了特殊处理.
有了富文本我们就可以轻松地将一连串文字进行自定义的设置,而不用使用多个Label去设置(这样会增大工作量)。