iOS YYText的使用笔记一(YYTextView图文编辑器)
来源:互联网 发布:淘宝订单号泄漏 编辑:程序博客网 时间:2024/05/19 18:43
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
看效果图:
(可编辑的文本+图片 有的app需要编辑文章功能 用这个就个可以大体实现了,图片和文本都已经获取到了 到时后对应传到服务器,之前没用YYTextview也实现过这种功能,效果比较差,用这个实现,相当完美,必须给YYText作者点个赞)
阅读全文
1 0
- iOS YYText的使用笔记一(YYTextView图文编辑器)
- iOS YYText的使用笔记一(YYTextView图文编辑器)
- iOS YYText的使用笔记二(YYLabel聊天表情+文字并排)
- iOS YYText的使用笔记二(YYLabel聊天表情+文字并排)
- iOS开发之YYKit丰富的组件,如:YYText
- YYText使用遇到的问题--文本无法点击
- YYText
- iOS YYText富文本开源库
- iOS开发学习之YYKit中YYText的深入解析,YYTextShadow的代码解析
- VI编辑器的使用笔记
- IOS总结_实现UIButton的图文混排(一)
- IOS总结_实现UIButton的图文混排(一)
- iOS开发中,Core Data的使用笔记(一)
- YYTextView显示服务器传输的评论文本方式
- iOS 使用 Cocoapods安装图文
- 浅析iOS中Push通知的使用【图文+视频】
- 浅析iOS中Push通知的使用【图文+视频】
- ListView的简单使用《一》—>普通图文展示案例
- 关于底部导航栏的跳转问题
- 移动端事件及事件应用
- 关于堆和栈的那些事
- Mysql5.6级联复制环境搭建
- Mysql函数INSTR、LOCATE、POSITION VS LIKE
- iOS YYText的使用笔记一(YYTextView图文编辑器)
- AngularJS Eclipse——新手入门【翻译+整理】
- 数据结构第四周项目-猴子选大王
- 北京市蛋糕相关业务分析
- 第十三周 项目二 二叉树排序树中查找的路径
- iOS安全攻与防
- ubuntu的一些常用配置
- 利用cout域宽实现数字时钟
- doc,docx,pdf,ppt等文件类型读取方法