写项目不免遇到使用富文本处理的时候,好比有的时候用户点击查看软件的某个功能说明,须要着重标注的部分要突出显示,有时候做为提示用语还要加上一个图片起到引导的做用提示用户该作什么或者完善什么等等,这个时候就要用到富文原本处理了。固然,也可使用coretext来处理也能够,简单的需求通常使用NSAttributedString彻底能够应付,使用coretext通常处理比较复杂的场景,还有使用NSAttributedString加载图片的时候是不支持gif图片的显示,只能显示静态图片,这点要认识到,因此若是处理相似斗鱼的弹幕那种仍是coretext会更完美。git
效果:github

本工具使用的快速,安全,方便安全
1,使用链式语法来处理富文本的样式添加,简单快速易懂app
2,两种方式处理富文本显示工具
使用时,只需加入XDYStringComponent.h和XDYStringComponent.m这俩文件便可学习
上代码:字体
第一种方式:优化
-(void)initViews{
self.view.backgroundColor = [UIColor whiteColor];
UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(20, 100, [UIScreen mainScreen].bounds.size.width-40, 300)];
bgView.layer.borderColor = [UIColor blackColor].CGColor;
bgView.layer.borderWidth = 1;
[self.view addSubview:bgView];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width-40, 100)];
label1.textAlignment = NSTextAlignmentCenter;
label1.numberOfLines = 0;
label1.lineBreakMode = NSLineBreakByWordWrapping;
[bgView addSubview:label1];
//第一种使用方式
XDYStringComponent *com1 = [[XDYStringComponent alloc] init];
com1.COMText(@"第一段文字红色,").COMFont([UIFont systemFontOfSize:14]).COMColor([UIColor redColor]).COMLineSpace(@(10)).COMSeperateSpace(@(4));
XDYStringComponent *com2 = [[XDYStringComponent alloc] init];
com2.COMText(@"第二段文字是黑色,").COMFont([UIFont systemFontOfSize:18]).COMColor([UIColor blackColor]).COMLineSpace(@(10)).COMSeperateSpace(@(4));
XDYStringComponent *com3 = [[XDYStringComponent alloc] init];
com3.COMText(@"第三段文字是蓝色,").COMFont([UIFont systemFontOfSize:12]).COMColor([UIColor blueColor]).COMLineSpace(@(10)).COMSeperateSpace(@(4));
XDYStringComponent *com4 = [[XDYStringComponent alloc] init];
com4.COMText(@"第四段文字是黄色加图片").COMFont([UIFont systemFontOfSize:12]).COMAttachImage([UIImage imageNamed:@"t4_chexian_title_baoxian"]).COMLineSpace(@(10)).COMSeperateSpace(@(4));
XDYStringComponent *com = [[XDYStringComponent alloc] init];
[com appendingStringWithString:[com1 appendingStringWithString:[com2 appendingStringWithString:[com3 appendingStringWithString:com4]]]];
//3连上4做为一个段落 2连上3和4的结合做为一个段落 1连上2和3的结合做为一个新的段落。最后赋值给label
label1.attributedText = com.attribuString;
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, [UIScreen mainScreen].bounds.size.width-40, 100)];
label2.textAlignment = NSTextAlignmentCenter;
label2.numberOfLines = 0;
label2.lineBreakMode = NSLineBreakByWordWrapping;
[bgView addSubview:label2];
XDYStringComponent *newCom = [XDYStringComponent addComponentWithStyleArray:@[
@{@"font":[UIFont systemFontOfSize:12],@"text":@"第一段文字红色,",@"color":[UIColor redColor]},
@{@"font":[UIFont systemFontOfSize:16],@"text":@"第二段文字是蓝色,",@"color":[UIColor blueColor]},
@{@"font":[UIFont systemFontOfSize:18],@"text":@"第二段文字是默认的白色,"},
@{@"font":[UIFont systemFontOfSize:14],@"text":@"第四段文字是黄色加图片",@"attach":[UIImage imageNamed:@"t4_share_icon_weixin"],@"color":[UIColor cyanColor]}
]];
label2.attributedText = newCom.attribuString;
}
能够看到,当须要定义富文本的样式时,直接链式语法拼接便可,自由设置字体大小,颜色,阴影,行间距,字间距,等等label可使用的属性。cdn
固然这种方式使用的时候有局限性,就是当我有大量文字的时候要加各类不一样的样式颜色,就要实例化多个stringComponent对象。而后一个个拼接,稍显麻烦,对此我写了个方法作了个优化,就是第二种方式对象
第二种方式:
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, [UIScreen mainScreen].bounds.size.width-40, 100)];
label2.textAlignment = NSTextAlignmentCenter;
label2.numberOfLines = 0;
label2.lineBreakMode = NSLineBreakByWordWrapping;
[bgView addSubview:label2];
XDYStringComponent *newCom = [XDYStringComponent addComponentWithStyleArray:@[
@{@"font":[UIFont systemFontOfSize:12],@"text":@"第一段文字红色,",@"color":[UIColor redColor]},
@{@"font":[UIFont systemFontOfSize:16],@"text":@"第二段文字是蓝色,",@"color":[UIColor blueColor]},
@{@"font":[UIFont systemFontOfSize:18],@"text":@"第二段文字是默认的白色,"},
@{@"font":[UIFont systemFontOfSize:14],@"text":@"第四段文字是黄色加图片",@"attach":[UIImage imageNamed:@"t4_share_icon_weixin"],@"color":[UIColor cyanColor]}
]];
label2.attributedText = newCom.attribuString;
这里封装了一个方法,方法里将显示的样式定义成固定格式的字典,键名就是特征值,font,color,等等,具体能够进入实现文件内部方法里查看,总共没几个键名,须要完善能够本身添加就好了
附上连接:github.com/dota4app/St…
有什么不足的地方欢迎你们指正,我仍是个新手,须要学习的地方不少,以后会不断完善