- 富文本的简单处理
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 60, CGRectGetWidth(self.view.frame), 60)];
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 200, CGRectGetWidth(self.view.frame), 60)];
[self.view addSubview:label];
[self.view addSubview:label1];
label.attributedText =
[[NSAttributedString alloc]initWithString:@"just test"
attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],//字体设置
NSForegroundColorAttributeName:[UIColor redColor],//字体颜色
NSBackgroundColorAttributeName:[UIColor greenColor],//背影颜色,默认为nil,透明色
NSKernAttributeName:@0.2, //字符间距,正值增加,负值减少
NSStrikethroughStyleAttributeName:@1,//删除线,默认为0
NSStrikethroughColorAttributeName:[UIColor orangeColor],//删除线颜色
//NSUnderlineStyleSingle
//NSUnderlineStyleDouble
NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),// 下划线,默认为0
NSUnderlineColorAttributeName:[UIColor blueColor],//下划线颜色
NSStrokeWidthAttributeName:@3.0,//笔画宽度,负值为填充效果,正值为中空效果
NSStrokeColorAttributeName:[UIColor grayColor],//暂无效果
NSBaselineOffsetAttributeName:@-3,//基线偏移值
NSObliquenessAttributeName:@0.1, //字符倾斜值
NSExpansionAttributeName:@0.5,//文本横向拉伸
}];
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString:@"just test"];
[attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
[attributeString addAttribute:NSStrikethroughStyleAttributeName value:@1 range:NSMakeRange(2, 3)];
label1.attributedText = attributeString;
2.常用方法的封装
//对字符串部分文本更改颜色
-(NSMutableAttributedString *)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;
}
//更改部分文字字体及颜色
-(NSMutableAttributedString *)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;
}
//更改字间距
-(NSMutableAttributedString *)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;
}
//更改行间距
-(NSMutableAttributedString *)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;
}
//更改字间距与行间距
-(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;
}