iOS 富文本编辑工厂, 让书写更简便.
由于最近常用富文本, 在编辑一个富文本时需要操作很多的属性, 书写起来很不方便. 所以我将这些相关属性整理并使用链式方式将它简化了一下. 效果请看下面Demo.
项目工程 实现很简单, 我嘴太笨, 这里就不介绍了, 如有兴趣直接看源码吧. 同时可以通过cocoapods
来使用它.
pod 'SJAttributesFactory'
附关于富文本的一些属性.
上下图文效果:
上下图文.jpg
之前:
// 文本字典NSDictionary *titleDict = @{NSFontAttributeName: [UIFont systemFontOfSize:fontSize],NSForegroundColorAttributeName: titleColor};NSDictionary *spacingDict = @{NSFontAttributeName: [UIFont systemFontOfSize:spacing]};// 图片文本NSTextAttachment *attachment = [[NSTextAttachment alloc] init];attachment.image = image;attachment.bounds = CGRectMake(0, 0, imageW, imageH);NSAttributedString *imageText = [NSAttributedString attributedStringWithAttachment:attachment];// 换行文本NSAttributedString *lineText = [[NSAttributedString alloc] initWithString:@"\n\n" attributes:spacingDict];// 按钮文字NSAttributedString *text = [[NSAttributedString alloc] initWithString:title attributes:titleDict];// 合并文字NSMutableAttributedString *attM = [[NSMutableAttributedString alloc] initWithAttributedString:imageText];[attM appendAttributedString:lineText];[attM appendAttributedString:text];
现在:
[SJAttributesFactory alteringStr:@"9999" task:^(SJAttributesFactory * _Nonnull worker) {worker.insertText(@"\n", 0).insertImage([UIImage imageNamed:@"sample2"], CGPointZero, CGSizeMake(50, 50), 0).lineSpacing(8) // 加点行间隔.alignment(NSTextAlignmentCenter).font([UIFont boldSystemFontOfSize:14]).fontColor([UIColor whiteColor]);}];
左缩进 + 右缩进
左缩进 + 右缩进.jpeg
之前:
NSString *str = @"故事:可以解释为旧事、旧业、先例、典故等涵义,同时,也是文学体裁的一种,侧重于事情过程的描述,强调情节跌宕起伏,从而阐发道理或者价值观。";NSMutableAttributedString *attrM = [[NSMutableAttributedString alloc] initWithString:str];[attrM addAttribute:NSFontAttributeNamevalue:[UIFont boldSystemFontOfSize:14]range:NSMakeRange(0, 3)];NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];style.firstLineHeadIndent = 8;style.headIndent = [[attrM attributedSubstringFromRange:NSMakeRange(0, 3)]boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeadingcontext:nil].size.width + style.firstLineHeadIndent;style.tailIndent = -8;[attrM addAttribute:NSParagraphStyleAttributeNamevalue:stylerange:NSMakeRange(0, str.length)];
现在:
[SJAttributesFactory alteringStr:@"故事:可以解释为旧事、旧业、先例、典故等涵义,同时,也是文学体裁的一种,侧重于事情过程的描述,强调情节跌宕起伏,从而阐发道理或者价值观。" task:^(SJAttributesFactory * _Nonnull worker) {worker.nextFont([UIFont boldSystemFontOfSize:14]).range(NSMakeRange(0, 3));CGFloat startW = worker.width(NSMakeRange(0, 3));worker.firstLineHeadIndent(8).headIndent(startW + 8).tailIndent(-8); }];
下划线 + 删除线
下划线 + 删除线.jpg
之前:
NSString *price = @"$ 999";NSMutableAttributedString *attrM = [[NSMutableAttributedString alloc] initWithString:price];NSRange range = NSMakeRange(0, price.length);[attrM addAttribute:NSFontAttributeNamevalue:[UIFont systemFontOfSize:40]range:range];[attrM addAttribute:NSUnderlineStyleAttributeNamevalue:@(NSUnderlineByWord | NSUnderlinePatternSolid | NSUnderlineStyleDouble)range:range];[attrM addAttribute:NSUnderlineColorAttributeNamevalue:[UIColor yellowColor]range:range];[attrM addAttribute:NSStrikethroughStyleAttributeNamevalue:@(NSUnderlineByWord | NSUnderlinePatternSolid | NSUnderlineStyleDouble)range:range];[attrM addAttribute:NSStrikethroughColorAttributeNamevalue:[UIColor redColor]range:range];
现在:
[SJAttributesFactory alteringStr:@"$ 999" task:^(SJAttributesFactory * _Nonnull worker) {worker.font([UIFont systemFontOfSize:40]);worker.underline(NSUnderlineByWord | NSUnderlinePatternSolid | NSUnderlineStyleDouble, [UIColor yellowColor]).strikethrough(NSUnderlineByWord | NSUnderlinePatternSolid | NSUnderlineStyleDouble, [UIColor redColor]);}];
Other
ex.gif
作者:changsanjiang
链接:https://juejin.im/post/5a06a4456fb9a0451463834b
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
iOS 富文本编辑工厂, 让书写更简便.相关推荐
- iOS富文本解析html图片,小程序解析html和富文本编辑内容【亲测有效】
首先去 https://github.com/icindy/wxParse 下载wxParse,只拷贝wxParse文件夹即可. 1.引入wxss @import "../../util/w ...
- html实现富文本编辑器,前端程序员福利,6款轻量级富文本编辑器,轻松实现富文本编辑...
1.国产富文本编辑wangEditor 基于javascript跟css开发的 Web富文本编辑器, 轻量.简洁.易用.开源免费,样式可自定义typecho 富文本编辑器,菜单栏可以自定义配置. 下载 ...
- android如何展示富文本_android高仿今日头条富文本编辑(发布文章)
前言: 在经历了几个月的项目期限.我们遇到了前端发布文章,要用到富文本编辑的功能.在一番衡量下最终用到了richeditor-android第三方框架.实现原理就是通过webView和js实现前端富文 ...
- 富文本编辑_博客的后台富文本编辑和阅读计数
哈喽,大家好呀,我是滑稽君.本期我们为博客加上分类统计功能.后台富文本编辑功能.和阅读计数功能.那么我们先说说富文本编辑器是什么. 富文本编辑器,Rich Text Editor, 简称 RTE, 它 ...
- android 上下滚动文字_android高仿今日头条富文本编辑(发布文章)
前言 在经历了几个月的项目期限.我们遇到了前端发布文章,要用到富文本编辑的功能.在一番衡量下最终用到了[richeditor-android](https://github.com/wasabeef/ ...
- swift 富文本编辑_如何使用Swift构建协作式文本编辑器
swift 富文本编辑 by Neo Ighodaro 由新Ighodaro 如何使用Swift构建协作式文本编辑器 (How to build a collaborative text editor ...
- android 仿写开发者头条,android高仿今日头条富文本编辑(发布文章)
前言: 在经历了几个月的项目期限.我们遇到了前端发布文章,要用到富文本编辑的功能.在一番衡量下最终用到了richeditor-android第三方框架.实现原理就是通过webView和js实现前端富文 ...
- html 富文本编辑(WYSIWYG)
HTML 富文本编辑 1.富文本编辑有两种形式:第一种:iframe 设置属性designMode="on" ,designMode 的默认值"off" :第二 ...
- iOS富文本组件的实现—DTCoreText源码解析 数据篇
本文转载 http://blog.cnbang.net/tech/2630/ DTCoreText是个开源的iOS富文本组件,它可以解析HTML与CSS最终用CoreText绘制出来,通常用于在一些需 ...
最新文章
- 2009年出现的计算机术语,2009年计算机一级考试真题及答案
- 我觉得要技术者上升到整体去考虑会好点
- 我花 1 分钟写了一段爬虫,帮助小姐姐解放了双手
- [七]RabbitMQ-客户端源码之AMQPImpl+Method
- linux中使用yum的优点,linux – 自动“yum update”以保证服务器安全 – 优点和缺点?...
- java文件编译为class文件需要键入什么命令_cmd命令行 编译Java 文件
- New Currency Rate IOS APP 上线
- MongoDB的安装和启动方法详解
- Java堆分配参数总结
- 程序员的离职小技巧之如何写出让接班同事无法维护的超级代码!建议收藏
- Altium designer--DB接口DB9/DB15/DB25/DB37/DB50
- sql 给字段设置默认值,
- 华为网络工程师终极面试题
- ISDA发布亚洲衍生品调查报告
- Kafka生产者、消费者的消息可靠性方案实现
- 深度理解相机中的各个参数(对比度、饱和度、亮度、曝光度、锐度)
- latex beamer template
- Python基础_第2章_Python运算符与if结构
- 戴蒙德对新冠疫情等的分析与看法笔记
- 苹果敢对“赞赏”分成30%真的是靠垄断吗?
热门文章
- Java和.NET使用DES对称加密的区别
- SPOJ AMR10I Dividing Stones --DFS
- 全球15个顶级技术类博客
- 【Smart_Point】unique_ptr与shared_ptr使用实例
- TCP性能和发送接收Buffer的关系
- Nginx源代码分析 - 日志处理
- Excel在.Net 环境下Web方式下驻留内存问题的解决
- asp.net中的联动菜单
- 以下一些使用ASP.NET和VISUAL STUDIO.NET2003的经验和技巧
- Spring源码分析【9】-SpringSecurity密码Remove原理