改变某些文字的颜色 并单独设置其字体
/**
* 改变某些文字的颜色 并单独设置其字体
*
* @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;
}
同时更改行间距和字间距
/**
* 同时更改行间距和字间距
*
* @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 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;
}
单纯改变句子的字间距(需要 <CoreText/CoreText.h>
/**
* 单纯改变句子的字间距(需要 <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;
}
单纯改变一句话中的某些字的颜色(一种颜色)
/**
* 单纯改变一句话中的某些字的颜色(一种颜色)
*
* @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;
}
相关各属性对应值:
NSFontAttributeName :字体字号value值:UIFont类型
NSParagraphStyleAttributeName : 段落样式value值:NSParagraphStyle类型(其属性如下)
lineSpacing 行间距(具体用法可查看上面的设置行间距API)
paragraphSpacing 段落间距
alignment 对齐方式
firstLineHeadIndent 指定段落开始的缩进像素
headIndent 调整全部文字的缩进像素
NSForegroundColorAttributeName 字体颜色value值:UIColor类型
NSBackgroundColorAttributeName 背景颜色value值:UIColor类型
NSObliquenessAttributeName 字体粗倾斜value值:NSNumber类型
NSExpansionAttributeName 字体加粗value值:NSNumber类型(比例) 0就是不变 1增加一倍
NSKernAttributeName 字间距value值:CGFloat类型
NSUnderlineStyleAttributeName 下划线value值:1或0
NSUnderlineColorAttributeName 下划线颜色value值:UIColor类型
NSStrikethroughStyleAttributeName 删除线value值:1或0
NSStrikethroughColorAttributeName 删除线颜色value值:UIColor类型
NSStrokeColorAttributeName 字体颜色value值:UIColor类型
NSStrokeWidthAttributeName 字体描边value值:CGFloat
NSLigatureAttributeName 连笔字value值:1或0
NSShadowAttributeName 阴影value值:NSShawdow类型(下面是其属性)
shadowOffset 影子与字符串的偏移量
shadowBlurRadius 影子的模糊程度
shadowColor 影子的颜色
NSTextEffectAttributeName 设置文本特殊效果,目前只有图版印刷效果可用value值:NSString类型
NSAttachmentAttributeName 设置文本附件value值:NSTextAttachment类型(没研究过,可自行百度研究)
NSLinkAttributeName 链接value值:NSURL (preferred) or NSString类型
NSBaselineOffsetAttributeName 基准线偏移value值:NSNumber类型
NSWritingDirectionAttributeName 文字方向 分别代表不同的文字出现方向value值:@[@(1),@(2)]