UILabel和UIButton原理是一样的,都是借助富文本,以UILabel为例
//假如我们的需求是将“注册”两个字设置为红色,“同意”设置为绿色
NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:@"点击注册按钮,即表示您已同意隐私条款和服务协议"];
NSRange redRange = NSMakeRange([[noteStr string] rangeOfString:@"注册"].location, [[noteStr string] rangeOfString:@"注册"].length);
[noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:redRange];
NSRange greenRange = NSMakeRange([[noteStr string] rangeOfString:@"同意"].location, [[noteStr string] rangeOfString:@"同意"].length);
[noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:greenRange];
[noteLabel setAttributedText:noteStr];
[noteLabel sizeToFit];