NSAttributedString解析
一、常用需求封装
需求:在我们日常开发中,某些句子中会有改变某些字颜色的需求,当然颜色一般而言就是为了着重强调,常为同一种颜色,所以下面代码是单纯改变一句话中的某些字的颜色(一种颜色)
/**
* 单纯改变一句话中的某些字的颜色(一种颜色)
*
* @param color 需要改变成的颜色
* @param totalStr 总的字符串
* @param subArray 需要改变颜色的文字数组(要是有相同的 只取第一个)
*
* @return 生成的富文本
*/
+ (NSMutableAttributedString *)ls_changeCorlorWithColor:(UIColor *)color TotalString:(NSString *)totalStr SubStringArray:(NSArray *)subArray {
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalStr];
for (NSString *rangeStr in subArray) {
NSRange range = [totalStr rangeOfString:rangeStr options:NSBackwardsSearch];
[attributedStr addAttribute:NSForegroundColorAttributeName value:color range:range];
}
return attributedStr;
}
需求:需要更改字间距来适应整体UI
/**
* 单纯改变句子的字间距(需要 《CoreText/CoreText.h》)
*
* @param totalString 需要更改的字符串
* @param space 字间距
*
* @return 生成的富文本
*/
+ (NSMutableAttributedString *)ls_changeSpaceWithTotalString:(NSString *)totalString Space:(CGFloat)space {
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalString];
long number = space;
CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
[attributedStr addAttribute:(id)kCTKernAttributeName value:(__bridge id)num range:NSMakeRange(0,[attributedStr length])];
CFRelease(num);
return attributedStr;
}
需求:需要改变行间距来适应整体UI
/**
* 单纯改变段落的行间距
*
* @param totalString 需要更改的字符串
* @param lineSpace 行间距
*
* @return 生成的富文本
*/
+ (NSMutableAttributedString *)ls_changeLineSpaceWithTotalString:(NSString *)totalString LineSpace:(CGFloat)lineSpace {
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalString];
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:lineSpace];
[attributedStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [totalString length])];
return attributedStr;
}
需求:同时更改行间距和字间距
/**
* 同时更改行间距和字间距
*
* @param totalString 需要改变的字符串
* @param lineSpace 行间距
* @param textSpace 字间距
*
* @return 生成的富文本
*/
+ (NSMutableAttributedString *)ls_changeLineAndTextSpaceWithTotalString:(NSString *)totalString LineSpace:(CGFloat)lineSpace textSpace:(CGFloat)textSpace {
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalString];
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:lineSpace];
[attributedStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [totalString length])];
long number = textSpace;
CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
[attributedStr addAttribute:(id)kCTKernAttributeName value:(__bridge id)num range:NSMakeRange(0,[attributedStr length])];
CFRelease(num);
return attributedStr;
}
需求:更改某些文字的颜色并修改其字体,突出重点强调
/**
* 改变某些文字的颜色 并单独设置其字体
*
* @param font 设置的字体
* @param color 颜色
* @param totalString 总的字符串
* @param subArray 想要变色的字符数组
*
* @return 生成的富文本
*/
+ (NSMutableAttributedString *)ls_changeFontAndColor:(UIFont *)font Color:(UIColor *)color TotalString:(NSString *)totalString SubStringArray:(NSArray *)subArray {
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalString];
for (NSString *rangeStr in subArray) {
NSRange range = [totalString rangeOfString:rangeStr options:NSBackwardsSearch];
[attributedStr addAttribute:NSForegroundColorAttributeName value:color range:range];
[attributedStr addAttribute:NSFontAttributeName value:font range:range];
}
return attributedStr;
非常好我支持^.^
(0) 0%
不好我反对
(0) 0%