NSMutableAttributedString的使用
在iOS开发中,对NSMutableAttributedString的使用是非常多的
比如:同行lable不同字体,不同颜色,下划线,删除线,以及同行label中间字体颜色不同,这些都是很常见的
同行不同字体
/**
同行label不同字体
*/
+(NSMutableAttributedString *)makeStringWith:(NSString *)textStr AndfirstColor:(UIColor *)firstColor AndsecondColor:(UIColor *)secondColor WithfirstFont:(CGFloat )firstFont andsecondFont:(CGFloat)secondFont andLastRange:(NSInteger)lastNumber{
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:textStr];
NSRange range1;
NSRange range2;
range1 = NSMakeRange(0, attr.length - lastNumber);
range2 = NSMakeRange(attr.length - lastNumber, lastNumber);
[attr addAttribute:NSForegroundColorAttributeName value:firstColor range:range1];
[attr addAttribute:NSForegroundColorAttributeName value:secondColor range:range2];
//使用特定的字体
// [attr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"AppleSDGothicNeo-Light" size:firstFont] range:range1];
// [attr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"AppleSDGothicNeo-Light" size:secondFont] range:range2];
[attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:firstFont] range:range1];
[attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:secondFont] range:range2];
return attr;
}
中间单一染色
/**
中间单一染色
*/
+(NSMutableAttributedString *)changeStringWith:(NSString *)oldStr andColor:(NSString *)hexStr andLenght:(NSInteger )lengh{
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:oldStr];
[attr addAttribute:NSForegroundColorAttributeName value:[UIColor colorFromHexString:hexStr] range:NSMakeRange(4, lengh)];
//颜色方法为16进制颜色复制,在前面的博客中介绍过
return attr;
}
或者用下面这种方法,同行label不同字体不同颜色,可以具体区分
+(NSMutableAttributedString *)makeStringWith:(NSString *)textStr AndfirstColor:(UIColor *)firstColor AndsecondColor:(UIColor *)secondColor WithfirstFont:(CGFloat )firstFont andsecondFont:(CGFloat)secondFont andThirdColor:(UIColor *)thirdColor andfirstRange:(NSRange )firstRange midRange:(NSRange )midRange lastRange:(NSRange)lastRange{
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:textStr];
[attr addAttribute:NSForegroundColorAttributeName value:firstColor range:firstRange];
[attr addAttribute:NSForegroundColorAttributeName value:secondColor range:midRange];
[attr addAttribute:NSForegroundColorAttributeName value:thirdColor range:lastRange];
[attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:firstFont] range:firstRange];
[attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:secondFont] range:midRange];
[attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:firstFont] range:lastRange];
return attr;
}
添加下划线
+ (NSMutableAttributedString *)makeALineUnderLabelWithStr:(NSString *)strr andRange:(NSRange)rangee{
NSMutableAttributedString *mustrr = [[NSMutableAttributedString alloc]initWithString:strr];
[mustrr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:rangee];
[mustrr addAttribute:NSForegroundColorAttributeName value:kRGB(108, 99, 255) range:rangee];
//设置下划线颜色...
[mustrr addAttribute:NSUnderlineColorAttributeName value:kRGB(108, 99, 255) range:rangee];
return mustrr;
}