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;

}



 

你可能感兴趣的:(iOS 富文本label字体大小和颜色)