iOS开发UI篇 —— 富文本NSAttributedString简介

一、处理普通文本的颜色

NSString *moneyStr= @"总冠军";
NSString *nameStr = @"勇士";
self.label  = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 300, 200)];
[self.label setTextColor:[UIColor purpleColor]];
[self.view addSubview:self.label];
//1.创建富文本,并设置富文本文字
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@2017年%@",nameStr,moneyStr]];
//2.设置富文本文字颜色及文字位置
[attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor cyanColor] range:NSMakeRange(attrStr.length-moneyStr.length-1, moneyStr.length)];
//3.将富文本设置给label
self.label.attributedText = attrStr;
详见图1效果图:

图1:

二、处理html中文字颜色

未完待整理

  

原文地址:https://www.cnblogs.com/TheYouth/p/7007229.html