iOS lable内容格式的设置

一个label中设置的文本含有2种以上不同的格式,又不能把它拆解为两个label来显示,这时用NSMutableAttributedString可以很好的解决问题。

NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString: label.attributedText];

    [text addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range: NSMakeRange(10, 1)];

    [label setAttributedText: text];

 

设置不同字体颜色

-(void)fuwenbenLabel:(UILabel *)labell FontNumber:(id)font AndRange:(NSRange)range AndColor:(UIColor *)vaColor

{

    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:labell.text];

    

    //设置字号

    [str addAttribute:NSFontAttributeName value:font range:range];

    

    //设置文字颜色

    [str addAttribute:NSForegroundColorAttributeName value:vaColor range:range];

    

    labell.attributedText = str;

}

 

 

 

 

 

posted @ 2015-08-20 09:25  songkl  阅读(233)  评论(0编辑  收藏  举报