简单的富文本使用工具

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

效果:

本工具使用的快速,安全,方便

1,使用链式语法来处理富文本的样式添加,简单快速易懂

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可以使用的属性。

当然这种方式使用的时候有局限性,就是当我有大量文字的时候要加各种不同的样式颜色,就要实例化多个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,等等,具体可以进入实现文件内部方法里查看,总共没几个键名,需要完善可以自己添加就行了

附上链接:https://github.com/dota4app/StringComponent.git

有什么不足的地方欢迎大家指正,我还是个新手,需要学习的地方很多,之后会不断完善

 

其他文章请查看个人博客:http://zhangqq166.cn/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用TinyMCE文本编辑器的简单案例: 1. 在HTML文件中引入TinyMCE的JavaScript文件和CSS文件。 ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>TinyMCE Example</title> <script src="tinymce/tinymce.min.js"></script> <link rel="stylesheet" href="tinymce/skins/lightgray/skin.min.css"> </head> <body> <textarea id="myTextarea"></textarea> <script> // 初始化TinyMCE编辑器 tinymce.init({ selector: '#myTextarea', skin: 'lightgray', height: 500, plugins: [ 'advlist autolink lists link image charmap print preview anchor', 'searchreplace visualblocks code fullscreen', 'insertdatetime media table contextmenu paste code help wordcount' ], toolbar: 'undo redo | formatselect | bold italic backcolor | \ alignleft aligncenter alignright alignjustify | \ bullist numlist outdent indent | removeformat | help' }); </script> </body> </html> ``` 2. 在JavaScript中初始化TinyMCE编辑器,设置编辑器的选项和工具栏。 ```javascript tinymce.init({ selector: '#myTextarea', // 编辑器要替换的元素的ID skin: 'lightgray', // 编辑器的皮肤 height: 500, // 编辑器的高度 plugins: [ 'advlist autolink lists link image charmap print preview anchor', 'searchreplace visualblocks code fullscreen', 'insertdatetime media table contextmenu paste code help wordcount' ], // 编辑器的插件 toolbar: 'undo redo | formatselect | bold italic backcolor | \ alignleft aligncenter alignright alignjustify | \ bullist numlist outdent indent | removeformat | help' // 编辑器的工具栏 }); ``` 3. 在页面中插入一个textarea元素,并设置其ID。 ```html <textarea id="myTextarea"></textarea> ``` 4. 运行页面,在textarea中输入文本,就可以使用工具栏对文本进行格式化、添加图片和链接等操作。 以上就是一个简单使用TinyMCE文本编辑器的案例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值