iOS 富文本label字体大小和颜色

 

相关链接:https://www.jianshu.com/p/ae795de53308

 

实现:最初实现的时候想到了用两个Label,来实现,第一个显示¥4000,设置一个字体,第二个显示/月,设置另一个字体.这样就能实现这个效果了,但是最后想一想还是用富文本比较好,顺便可以学习一下.

 

//先创建一个label:

-(UILabel *)priceLabel{

 if (_priceLabel == nil)

{

_priceLabel = [[UILabel alloc]init];

_priceLabel.font = kFONT(13);

_priceLabel.textColor = kColorTheme;

_priceLabel.textAlignment = NSTextAlignmentRight;

}

return _priceLabel;

}

 

 

_priceLabel.attributedText = [self getPriceAttribute:@"¥4000/月"]; 

 

 

-(NSMutableAttributedString *)getPriceAttribute:(NSString *)string{

NSMutableAttributedString *attribut = [[NSMutableAttributedString alloc]initWithString:string];

//目的是想改变 ‘/’前面的字体的属性,所以找到目标的range

NSRange range = [string rangeOfString:@"/"];

NSRange pointRange = NSMakeRange(0, range.location);

NSMutableDictionary *dic = [NSMutableDictionary dictionary];

dic[NSFontAttributeName] = [UIFont systemFontOfSize:18];

//赋值 [attribut addAttributes:dic range:pointRange];

return attribut;

}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值