关于NSMutableAttributedString属性字符串的用法

  • 今天接触了属性字符串的部分用法,发现真是好用啊!,以下是部分用法:
NSMutableAttributedString* attributedStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"合计: ¥%.2f",model.goodsCount * model.goodsModel.price]];

NSRange range1 = NSMakeRange(0, 3);

NSRange range2 = NSMakeRange(3, attributedStr.length - 3);

[attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#6c6c6c"] range:range1];//设置前面三个字符的颜色

[attributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14.f] range:range1];//设置前面三个字符的字号大小

[attributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18.f] range:range2];//设置后面字符的颜色

[attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#fc4001"] range:range2];//设置后面字符的字号大小

UILable* label=[ [UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)]];

label.attributedText = attributedStr;

效果图如下:属性字符串实现的效果
- 这样就可以把可变的字符串搞到label上了,另外还有加横穿线、下划线等多种用法。等着你去开发。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值