iOS 中为同一个label设置不同的颜色

来源:互联网 发布:中国制造的实力 知乎 编辑:程序博客网 时间:2024/05/18 20:04

NSString *mString = @"我是一个label,我有各种颜色";

NSMutableAttributedString *attributedString = [[NSMutableAttributedStringalloc]initWithString:mString];


//分别为指定的区间设置不同的颜色

//RGB(85,108,133) 是[UIColor colorWithRed...]宏定义

[str addAttribute:NSForegroundColorAttributeNamevalue:RGB(85,108,133) range:NSMakeRange(0,mStringTransmitted.length -4)];

[str addAttribute:NSForegroundColorAttributeNamevalue:RGB(210,210,210) range:NSMakeRange(mStringTransmitted.length -4,4)];


self.label.attributedText = attributedString;


1 0
原创粉丝点击