iOS YYText的使用笔记一(YYTextView图文编辑器)
来源:互联网 发布:mac怎么还原出厂设置 编辑:程序博客网 时间:2024/05/07 13:50
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 ()@endYYTextView *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作者点个赞)
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的简单使用《一》—>普通图文展示案例
- 深入分析 Java 中的中文编码问题
- hdu 5643 (约瑟夫环加强版)
- CSS左侧固定宽 右侧自适应(兼容所有浏览器)
- 第34讲项目1——分数的累加
- Scala学习笔记--第5章 映射(Map)
- iOS YYText的使用笔记一(YYTextView图文编辑器)
- MYSQL校对规则
- View 的 OverScrollBy 方法
- io
- Learning to Rank简介
- UML之活动图
- Android中从视频中提取音频
- 对照Java学习Swift--基础部分
- 函数模板(Function Template)及带默认参数的函数 http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=25520556&id