一、UILabel和UIButton加载html文本
NSString *htmlStr = @\"<b style=\'color: #0055cc;\'>测试</b><b style=\'color: #0055cc;\'>测试</b>\";NSMutableAttributedString *htmlAttStr = [[NSMutableAttributedString alloc] initWithData:[htmlStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];_titleLabel.attributedText = htmlAttStr;// UIButton 加载html文本[_button setAttributedTitle:htmlAttStr forState:UIControlStateNormal];// 注意不能使用如下方法, 亲测无效//_button.titleLabel.attributedText = htmlAttStr;
二、UILabel部分字体颜色变色
NSString *text = @\"什么时候才能刷出凤凰和无敌啊\";NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:text];NSRange range = [text rangeOfString:@\"凤凰和无敌\"];[attrStr addAttribute:NSForegroundColorAttributeName value:[ShareMethod colorWithHexString:@\"#0055cc\"]range:range];_titleLabel.attributedText = attrStr;