需求背景
需求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對象(整數(shù)),負值間距變窄村砂,正值間距變寬
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;
}
}