需求背景
需求1: 项目中有多个收货地址, 当其中的收货地址为默认地址的时候. 如下图

需求2: 项目中金额款项字体颜色变红

这时候用两个UILbl肯定是不合适的, 需要使用富文本属性修改, 考虑到项目中这种使用场景比较多, 所以为UILbl新增一个分类.
分类
UILabel+Extension.h
/**
* 富文本应用: 变色, 改变字体大小
*/
- (void)changeLblFont:(float)font textColor:(UIColor *)textColor range: (NSRange)range;
UILabel+Extension.m
- (void)changeLblFont:(float)font textColor:(UIColor *)textColor range: (NSRange)range{
if (self.text) {
// 富文本变色 NSForegroundColorAttributeName
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:self.text];
[AttributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font] range:range];
[AttributedStr addAttribute:NSForegroundColorAttributeName value:textColor range:range];
self.attributedText = AttributedStr;
}
}
使用
- 需求一:
[self.addressLbl changeLblFont:12 textColor:kRedTextColor range:NSMakeRange(0, 6)];
- 需求二:
提交成功:
[self.successLbl changeLblFont:15 textColor:kRedTextColor range:NSMakeRange(self.successLbl.text.length - 5, 5)];
金额字体变红:
[self.promptLbl changeLblFont:12 textColor:kRedTextColor range:NSMakeRange(self.promptLbl.text.length - str.length - 1, str.length)];
拓展
考虑到项目中其他对UILbl进行的操作: 改变字体间距、改变行间距、
- .h
/**
* 改变行间距 类方法
*/
+ (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space textAlignment:(NSTextAlignment)textAlignment;
/**
* 改变行间距
*/
- (void)changeLineSpaceForLabel:(float)space textAlignment:(NSTextAlignment)textAlignment;
/**
* 改变字间距 类方法
*/
+ (void)changeWordSpaceForLabel:(UILabel *)label WithSpace:(float)space textAlignment:(NSTextAlignment)textAlignment;
/**
* 改变字间距
*/
-(void)changeWordSpaceForLabel:(float)space textAlignment:(NSTextAlignment)textAlignment;
/**
* 改变行间距和字间距 类方法
*/
+ (void)changeSpaceForLabel:(UILabel *)label withLineSpace:(float)lineSpace WordSpace:(float)wordSpace textAlignment:(NSTextAlignment)textAlignment;
/**
* 改变行间距和字间距
*/
- (void)changeSpaceForLabel:(float)lineSpace WordSpace:(float)wordSpace textAlignment:(NSTextAlignment)textAlignment;
- .m
/**
* 改变行间距 类方法
*/
+ (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space textAlignment:(NSTextAlignment)textAlignment {
if (label.text) {
NSString *labelText = label.text;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:space];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
label.attributedText = attributedString;
[label sizeToFit];
label.textAlignment = textAlignment;
}
}
/**
* 改变行间距
*/
- (void)changeLineSpaceForLabel:(float)space textAlignment:(NSTextAlignment)textAlignment{
if (self.text) {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:space];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self.text length])];
self.attributedText = attributedString;
[self sizeToFit];
self.textAlignment = textAlignment;
}
}
/**
* 改变字间距 类方法
*/
+ (void)changeWordSpaceForLabel:(UILabel *)label WithSpace:(float)space textAlignment:(NSTextAlignment)textAlignment{
if (label.text) {
NSString *labelText = label.text;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(space)}];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
label.attributedText = attributedString;
[label sizeToFit];
label.textAlignment = textAlignment;
}
}
/**
* 改变字间距
*/
-(void)changeWordSpaceForLabel:(float)space textAlignment:(NSTextAlignment)textAlignment{
if (self.text) {
// 取值为NSNumber对象(整数),负值间距变窄,正值间距变宽
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.text attributes:@{NSKernAttributeName:@(space)}];
self.attributedText = attributedString;
[self sizeToFit];
self.textAlignment = textAlignment;
}
}
/**
* 改变行间距和字间距 类方法
*/
+ (void)changeSpaceForLabel:(UILabel *)label withLineSpace:(float)lineSpace WordSpace:(float)wordSpace textAlignment:(NSTextAlignment)textAlignment{
if (label.text) {
NSString *labelText = label.text;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(wordSpace)}];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:lineSpace];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
label.attributedText = attributedString;
[label sizeToFit];
label.textAlignment = textAlignment;
}
}
/**
* 改变行间距和字间距
*/
- (void)changeSpaceForLabel:(float)lineSpace WordSpace:(float)wordSpace textAlignment:(NSTextAlignment)textAlignment{
if (self.text) {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.text attributes:@{NSKernAttributeName:@(wordSpace)}];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:lineSpace];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self.text length])];
self.attributedText = attributedString;
[self sizeToFit];
self.textAlignment = textAlignment;
}
}
网友评论