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%