label的富文本设置
2016-07-08 22:40
447 查看
/**传入一个label,范围字符串的字体颜色为自定义的颜色*/
- (NSMutableAttributedString*)setMutableAttributesStringWithString:(UILabel *)label range:(NSRange)range
{
//范围字符串属性修改
NSDictionary *attributes =
@{NSForegroundColorAttributeName:label.textColor
};
//1.范围截取字符串
NSString *rangeString = [label.text
substringWithRange:range];
//2.将字符串改为可变属性字符串
NSMutableAttributedString *attributedString = [[NSMutableAttributedString
alloc]initWithString:label.text
attributes:attributes];
//3.设置属性和需要修改的文字范围
UIColor *black = [UIColor
blackColor];
NSRange redTextRange = [label.text
rangeOfString:rangeString];
//4.赋值给可变属性字符串
[attributedString setAttributes:@{NSForegroundColorAttributeName:black}
range:redTextRange];
return attributedString;
}
- (NSMutableAttributedString*)setMutableAttributesStringWithString:(UILabel *)label range:(NSRange)range
{
//范围字符串属性修改
NSDictionary *attributes =
@{NSForegroundColorAttributeName:label.textColor
};
//1.范围截取字符串
NSString *rangeString = [label.text
substringWithRange:range];
//2.将字符串改为可变属性字符串
NSMutableAttributedString *attributedString = [[NSMutableAttributedString
alloc]initWithString:label.text
attributes:attributes];
//3.设置属性和需要修改的文字范围
UIColor *black = [UIColor
blackColor];
NSRange redTextRange = [label.text
rangeOfString:rangeString];
//4.赋值给可变属性字符串
[attributedString setAttributes:@{NSForegroundColorAttributeName:black}
range:redTextRange];
return attributedString;
}
相关文章推荐
- 257. Binary Tree Paths
- SQL查询 生成XML
- 网络爬虫
- golang学习之mgo操作mongodb
- 实用代码块记录2
- syntax error, unexpected T_STRIN
- C 学习之大小端
- 前端组件化思维
- 浅谈Windows 7系统启动过程
- extern "C"
- (3)WSDL文档深入分析
- JS数组
- H.264之编码格式-封装成MP4格式-视频流 RTP封包
- haproxy配置文档
- php rabbitmq操作类及生产者和消费者实例代码
- 错误: Unable to find explicit activity class ...have you declared this activity in your AndroidMa
- css两种块状定长元素居中方法的不同点
- [置顶] hadoop无法正常关闭尝试处理方法
- LeetCode 064 Minimum Path Sum
- maya中实时置换效果 dx11shader