iOS YYText的使用笔记一(YYTextView图文编辑器)
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来拼接文字和图片(占位)
直接上代码:
- //
- // TextAndImageTextViewVC.h
- // YYTextDemo
- //
- // Created by linpeng on 16/3/13.
- // Copyright © 2016年 ibireme. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- @interface TextAndImageTextViewVC : UIViewController
- @end
- //
- // TextAndImageTextViewVC.m
- // YYTextDemo
- //
- // Created by linpeng on 16/3/13.
- // Copyright © 2016年 ibireme. All rights reserved.
- //
- #import "TextAndImageTextViewVC.h"
- #import "YYText.h"
- #import "UIView+YYAdd.h"
- #import "YYTextView.h"
- #import "YYImage.h"
- #import "NSBundle+YYAdd.h"
- #import "NSString+YYAdd.h"
- @interface TextAndImageTextViewVC ()
- @end
- YYTextView *textView;
- @implementation TextAndImageTextViewVC
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self.view setBackgroundColor:[UIColor whiteColor]];
- textView = [[YYTextView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
- textView.userInteractionEnabled = YES;
- textView.textVerticalAlignment = YYTextVerticalAlignmentTop;
- textView.size = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
- //创建最主要的attribute文本
- NSMutableAttributedString *contentText = [NSMutableAttributedString new];
- UIFont *font = [UIFont systemFontOfSize:16];
- //图片资源
- YYImage *image = [YYImage imageNamed:@"demo.jpg"];
- image.preloadAllAnimatedImageFrames = YES;
- //添加文本+图片
- [contentText appendAttributedString:[[NSAttributedString alloc] initWithString:@"这是第一站图片" attributes:nil]];
- {
- YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];
- imageView.frame = CGRectMake(0, 0, textView.width - 10, textView.width/image.size.width*image.size.height);
- NSMutableAttributedString *attachText = [NSMutableAttributedString yy_attachmentStringWithContent:imageView contentMode:UIViewContentModeScaleAspectFit attachmentSize:imageView.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
- [contentText appendAttributedString:attachText];
- }
- //添加文本+图片
- [contentText appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n 接下来是第二张" attributes:nil]];
- {
- YYAnimatedImageView *imageView2 = [[YYAnimatedImageView alloc] initWithImage:image];
- imageView2.frame = CGRectMake(0, 0, textView.width - 10, textView.width/image.size.width*image.size.height);
- NSMutableAttributedString *attachText2 = [NSMutableAttributedString yy_attachmentStringWithContent:imageView2 contentMode:UIViewContentModeScaleAspectFit attachmentSize:imageView2.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
- [contentText appendAttributedString:attachText2];
- }
- textView.attributedText = contentText;
- [self.view addSubview:textView];
- //获取图片资源
- NSArray *attachments = textView.textLayout.attachments;
- for(YYTextAttachment *attachment in attachments)
- {
- YYAnimatedImageView *imageView = attachment.content;
- YYImage *image = (YYImage *)imageView.image;
- NSLog(@"获取到图片:%@",image);
- }
- NSArray *attachmentRanges = textView.textLayout.attachmentRanges;
- for (NSValue *range in attachmentRanges)
- {
- NSRange r = [range rangeValue];
- NSLog(@"资源所在位置:%ld 长度: %ld",r.location,r.length);
- }
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- /*
- #pragma mark - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- @end
以上代码加上注释应该很容易理解就不多讲了 注意 \n 这个换行符的使用
看效果图:
(可编辑的文本+图片 有的app需要编辑文章功能 用这个就个可以大体实现了,图片和文本都已经获取到了 到时后对应传到服务器,之前没用YYTextview也实现过这种功能,效果比较差,用这个实现,相当完美,必须给YYText作者点个赞)
iOS YYText的使用笔记一(YYTextView图文编辑器)相关推荐
- yytextview多种格式_iOS YYText的使用笔记一(YYTextView图文编辑器)
YYText是强大的YYKit的一部分可以单独下载 Powerful text framework for iOS to display and edit rich text. (It's a com ...
- 【iOS开发每日小笔记(一)】UIPickerView 自动选择某个component的某个row
这篇文章是我的[iOS开发每日小笔记]系列中的一片,记录的是今天在开发工作中遇到的,可以用很短的文章或很小的demo演示解释出来的小心得小技巧.它们可能会给用户体验.代码效率得到一些提升,或是之前自己 ...
- 【iOS开发每日小笔记(二)】gitHub上的开源“瀑布流”使用心得
这篇文章是我的[iOS开发每日小笔记]系列中的一片,记录的是今天在开发工作中遇到的,可以用很短的文章或很小的demo演示解释出来的小心得小技巧.它们可能会给用户体验.代码效率得到一些提升,或是之前自己 ...
- iOS 实战开发课程笔记
iOS 实战开发课程笔记 本贴旨在作为对极客班 <iOS 开发实战>第五期期课程视频重新学习的笔记. 目标是建立一个比较完整的 iOS 开发知识点框架以及快速手册. 对各个内容的详细研究会 ...
- iOS App Clips学习笔记
一.什么是App Clip? App Clip是一个App的轻量版,用来提供一些用户所需功能,我们可以把它看成苹果的小程序.它不需要下载就直接能打开.这样即可达到不下载完整版APP便可体验APP的部分 ...
- ios学习--iphone开发笔记和技巧总结(原址持续更新)
ios学习--iphone开发笔记和技巧总结(原址持续更新) 分类: ios Object-C2012-04-18 10:16 2716人阅读 评论(1) 收藏 举报 uiviewiphonelist ...
- 【iOS开发每日小笔记(七)】UITextView UITextField 检测并过滤Emoji表情符号
这篇文章是我的[iOS开发每日小笔记]系列中的一片,记录的是今天在开发工作中遇到的,可以用很短的文章或很小的demo演示解释出来的小心得小技巧.它们可能会给用户体验.代码效率得到一些提升,或是之前自己 ...
- html5 canvas图文编辑器源码_5个微信编辑器,再也不用为公众号发愁啦
1.秀米编辑器 秀米图文排版:原创模板素材,精选风格排版,独一无二的排版方式,设计出只属于你的图文.秀米H5秀制作:丰富的页面模板,独有的秀米组件,无论是多页场景H5,还是长页图文H5,都能让你快速制 ...
- 开源微信管家平台——JeeWx 捷微4.0 微服务版本发布,全新架构,全新UI,提供强大的图文编辑器...
JeeWx捷微4.0 微服务版本发布^_^ 换代产品(全新架构,全新UI,提供强大的图文编辑器) JEEWX 从4.0版本开始,技术架构全新换代,采用微服务架构,插件式开发,每个业务模块都是独立的 ...
最新文章
- 欢迎参加2022年第一次《城市大脑建设标准规范》专家研讨会
- Android UI开发第五篇——自定义列表
- opencv-python(PIL)图像处理之训练模型前的几种图预处理
- Integer装箱和拆箱 以及扔玻璃球的题目
- JAVA基础——Switch条件语句
- PHP读取excel表格内容 PHP-ExcelReader
- BeanUtils的方法
- 两个年月日怎么相减_会议记录应该怎么记?看这里
- VC++ 6.0 中如何使用 CRT 调试功能来检测内存泄漏[转]
- [Python图像处理] 三十五.OpenCV图像处理入门、算数逻辑运算与图像融合(推荐)
- sqlserver导入向导时提示外部表不是预期格式_Excel办公实操,导入本地数据,创建参数查询,就是简单...
- 基于java springboot+mybatis OA办公自动化系统设计和实现
- 【xv6 RISC-V】xv6操作系统原理解析与源代码阅读报告
- Kubernetes 一键部署实践
- 中兴新支点Linux国产操作系统安装windows字体的方法
- MATLAB三维散点图的绘制函数详解(scatter3、plot3) (有示例)
- 自建nod32更新服务器,ESET NOD32镜像更新服务器
- 外汇EA量化交易,怎么提高交易水平
- win2003终端服务器超出了最大允许连接数解决方案
- 解释:为什么scanf被跳过或不执行