YYText是强大的YYKit的一部分可以单独下载

Github地址 :    https://github.com/ibireme/YYText

Powerful text framework for iOS to display and edit rich text.
(It's a component of YYKit)

集成的时候建议使用cocopod管理

1.里面主要两个控件:

YYTextView 和 YYLabel

现在主要是YYTextview的简单使用

YYText主要是NSMutableAttributedString来处理富文本 他的内部实现可以自己去深究。

简单的图文并排,使用NSMutableAttributedString 创建一个对象 然后 appendAttributesString来拼接文字和图片(占位)

直接上代码:

[html] view plain copy
  1. //
  2. //  TextAndImageTextViewVC.h
  3. //  YYTextDemo
  4. //
  5. //  Created by linpeng on 16/3/13.
  6. //  Copyright © 2016年 ibireme. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. @interface TextAndImageTextViewVC : UIViewController
  10. @end
[html] view plain copy
  1. //
  2. //  TextAndImageTextViewVC.m
  3. //  YYTextDemo
  4. //
  5. //  Created by linpeng on 16/3/13.
  6. //  Copyright © 2016年 ibireme. All rights reserved.
  7. //
  8. #import "TextAndImageTextViewVC.h"
  9. #import "YYText.h"
  10. #import "UIView+YYAdd.h"
  11. #import "YYTextView.h"
  12. #import "YYImage.h"
  13. #import "NSBundle+YYAdd.h"
  14. #import "NSString+YYAdd.h"
  15. @interface TextAndImageTextViewVC ()
  16. @end
  17. YYTextView *textView;
  18. @implementation TextAndImageTextViewVC
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. [self.view setBackgroundColor:[UIColor whiteColor]];
  22. textView = [[YYTextView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
  23. textView.userInteractionEnabled = YES;
  24. textView.textVerticalAlignment = YYTextVerticalAlignmentTop;
  25. textView.size = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
  26. //创建最主要的attribute文本
  27. NSMutableAttributedString *contentText = [NSMutableAttributedString new];
  28. UIFont *font = [UIFont systemFontOfSize:16];
  29. //图片资源
  30. YYImage *image = [YYImage imageNamed:@"demo.jpg"];
  31. image.preloadAllAnimatedImageFrames = YES;
  32. //添加文本+图片
  33. [contentText appendAttributedString:[[NSAttributedString alloc] initWithString:@"这是第一站图片" attributes:nil]];
  34. {
  35. YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];
  36. imageView.frame = CGRectMake(0, 0, textView.width - 10, textView.width/image.size.width*image.size.height);
  37. NSMutableAttributedString *attachText = [NSMutableAttributedString yy_attachmentStringWithContent:imageView contentMode:UIViewContentModeScaleAspectFit attachmentSize:imageView.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
  38. [contentText appendAttributedString:attachText];
  39. }
  40. //添加文本+图片
  41. [contentText appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n 接下来是第二张" attributes:nil]];
  42. {
  43. YYAnimatedImageView *imageView2 = [[YYAnimatedImageView alloc] initWithImage:image];
  44. imageView2.frame = CGRectMake(0, 0, textView.width - 10, textView.width/image.size.width*image.size.height);
  45. NSMutableAttributedString *attachText2 = [NSMutableAttributedString yy_attachmentStringWithContent:imageView2 contentMode:UIViewContentModeScaleAspectFit attachmentSize:imageView2.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
  46. [contentText appendAttributedString:attachText2];
  47. }
  48. textView.attributedText = contentText;
  49. [self.view addSubview:textView];
  50. //获取图片资源
  51. NSArray *attachments =  textView.textLayout.attachments;
  52. for(YYTextAttachment *attachment in attachments)
  53. {
  54. YYAnimatedImageView *imageView = attachment.content;
  55. YYImage *image = (YYImage *)imageView.image;
  56. NSLog(@"获取到图片:%@",image);
  57. }
  58. NSArray *attachmentRanges = textView.textLayout.attachmentRanges;
  59. for (NSValue *range in attachmentRanges)
  60. {
  61. NSRange r = [range rangeValue];
  62. NSLog(@"资源所在位置:%ld 长度: %ld",r.location,r.length);
  63. }
  64. }
  65. - (void)didReceiveMemoryWarning {
  66. [super didReceiveMemoryWarning];
  67. // Dispose of any resources that can be recreated.
  68. }
  69. /*
  70. #pragma mark - Navigation
  71. // In a storyboard-based application, you will often want to do a little preparation before navigation
  72. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  73. // Get the new view controller using [segue destinationViewController].
  74. // Pass the selected object to the new view controller.
  75. }
  76. */
  77. @end

以上代码加上注释应该很容易理解就不多讲了  注意 \n 这个换行符的使用

看效果图:

(可编辑的文本+图片 有的app需要编辑文章功能 用这个就个可以大体实现了,图片和文本都已经获取到了 到时后对应传到服务器,之前没用YYTextview也实现过这种功能,效果比较差,用这个实现,相当完美,必须给YYText作者点个赞)

iOS YYText的使用笔记一(YYTextView图文编辑器)相关推荐

  1. yytextview多种格式_iOS YYText的使用笔记一(YYTextView图文编辑器)

    YYText是强大的YYKit的一部分可以单独下载 Powerful text framework for iOS to display and edit rich text. (It's a com ...

  2. 【iOS开发每日小笔记(一)】UIPickerView 自动选择某个component的某个row

    这篇文章是我的[iOS开发每日小笔记]系列中的一片,记录的是今天在开发工作中遇到的,可以用很短的文章或很小的demo演示解释出来的小心得小技巧.它们可能会给用户体验.代码效率得到一些提升,或是之前自己 ...

  3. 【iOS开发每日小笔记(二)】gitHub上的开源“瀑布流”使用心得

    这篇文章是我的[iOS开发每日小笔记]系列中的一片,记录的是今天在开发工作中遇到的,可以用很短的文章或很小的demo演示解释出来的小心得小技巧.它们可能会给用户体验.代码效率得到一些提升,或是之前自己 ...

  4. iOS 实战开发课程笔记

    iOS 实战开发课程笔记 本贴旨在作为对极客班 <iOS 开发实战>第五期期课程视频重新学习的笔记. 目标是建立一个比较完整的 iOS 开发知识点框架以及快速手册. 对各个内容的详细研究会 ...

  5. iOS App Clips学习笔记

    一.什么是App Clip? App Clip是一个App的轻量版,用来提供一些用户所需功能,我们可以把它看成苹果的小程序.它不需要下载就直接能打开.这样即可达到不下载完整版APP便可体验APP的部分 ...

  6. ios学习--iphone开发笔记和技巧总结(原址持续更新)

    ios学习--iphone开发笔记和技巧总结(原址持续更新) 分类: ios Object-C2012-04-18 10:16 2716人阅读 评论(1) 收藏 举报 uiviewiphonelist ...

  7. 【iOS开发每日小笔记(七)】UITextView UITextField 检测并过滤Emoji表情符号

    这篇文章是我的[iOS开发每日小笔记]系列中的一片,记录的是今天在开发工作中遇到的,可以用很短的文章或很小的demo演示解释出来的小心得小技巧.它们可能会给用户体验.代码效率得到一些提升,或是之前自己 ...

  8. html5 canvas图文编辑器源码_5个微信编辑器,再也不用为公众号发愁啦

    1.秀米编辑器 秀米图文排版:原创模板素材,精选风格排版,独一无二的排版方式,设计出只属于你的图文.秀米H5秀制作:丰富的页面模板,独有的秀米组件,无论是多页场景H5,还是长页图文H5,都能让你快速制 ...

  9. 开源微信管家平台——JeeWx 捷微4.0 微服务版本发布,全新架构,全新UI,提供强大的图文编辑器...

    JeeWx捷微4.0   微服务版本发布^_^ 换代产品(全新架构,全新UI,提供强大的图文编辑器) JEEWX 从4.0版本开始,技术架构全新换代,采用微服务架构,插件式开发,每个业务模块都是独立的 ...

最新文章

  1. 欢迎参加2022年第一次《城市大脑建设标准规范》专家研讨会
  2. Android UI开发第五篇——自定义列表
  3. opencv-python(PIL)图像处理之训练模型前的几种图预处理
  4. Integer装箱和拆箱 以及扔玻璃球的题目
  5. JAVA基础——Switch条件语句
  6. PHP读取excel表格内容 PHP-ExcelReader
  7. BeanUtils的方法
  8. 两个年月日怎么相减_会议记录应该怎么记?看这里
  9. VC++ 6.0 中如何使用 CRT 调试功能来检测内存泄漏[转]
  10. [Python图像处理] 三十五.OpenCV图像处理入门、算数逻辑运算与图像融合(推荐)
  11. sqlserver导入向导时提示外部表不是预期格式_Excel办公实操,导入本地数据,创建参数查询,就是简单...
  12. 基于java springboot+mybatis OA办公自动化系统设计和实现
  13. 【xv6 RISC-V】xv6操作系统原理解析与源代码阅读报告
  14. Kubernetes 一键部署实践
  15. 中兴新支点Linux国产操作系统安装windows字体的方法
  16. MATLAB三维散点图的绘制函数详解(scatter3、plot3) (有示例)
  17. 自建nod32更新服务器,ESET NOD32镜像更新服务器
  18. 外汇EA量化交易,怎么提高交易水平
  19. win2003终端服务器超出了最大允许连接数解决方案
  20. 解释:为什么scanf被跳过或不执行

热门文章

  1. 给IDEA换一个酷炫的主题
  2. 记录ci框架中定时任务的执行
  3. 基于STM32的智能数据采集系统
  4. 5G NR Rel16 两步接入/2-step RACH
  5. 高精度 双目视觉方案
  6. DaZeng:前端准备
  7. 如何在Python中将数字文件存储在数据库中
  8. 信息技术等级考试试题及答案
  9. Hilbert变换和接收机(I)
  10. 二、cxf框架实现REST风格http协议的WebService(JAX-RS标准)