OC中的富文本

来源:互联网 发布:打扮家 知乎 编辑:程序博客网 时间:2024/05/01 05:48

富文本可以将一串文字上的内容根据需求设置成不同的字体或大小如:“我是中国”,我们将“我是中国人”中的“中国”设置成红色,而将“人”设置成较大的字体。

代码:

NSMutableAttributedString *AttributedStr = [[NSMutableAttributedStringalloc]initWithString:@"更多细则,请参见《会员礼遇条款》"];

    [AttributedStr addAttribute:NSFontAttributeName

                          value:Font12

                          range:NSMakeRange(9,6)];

    [AttributedStraddAttribute:NSForegroundColorAttributeName

                          value:color

                          range:NSMakeRange(9,6)];

    [AttributedStraddAttribute:NSForegroundColorAttributeName

                          value:Color153

                          range:NSMakeRange(0,9)];

    [AttributedStraddAttribute:NSForegroundColorAttributeName

                          value:Color153

                          range:NSMakeRange(15,1)];

    aboutBeniLbl.attributedText = AttributedStr

上述代码就将“会员礼遇条款”的颜色和大小做了特殊处理.

有了富文本我们就可以轻松地将一连串文字进行自定义的设置,而不用使用多个Label去设置(这样会增大工作量)。

1 0