700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > iOS YYText的使用笔记一(YYTextView图文编辑器)

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

时间:2019-12-23 06:28:27

相关推荐

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

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

Github地址 : /ibireme/YYText

Powerful text framework for iOS to display and edit rich text.

(It's a component ofYYKit)

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

1.里面主要两个控件:

YYTextView 和 YYLabel

现在主要是YYTextview的简单使用

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

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

直接上代码:

[html]view plain copy////TextAndImageTextViewVC.h//YYTextDemo////Createdbylinpengon16/3/13.//Copyright©ibireme.Allrightsreserved.//#import<UIKit/UIKit.h>@interfaceTextAndImageTextViewVC:UIViewController@end

[html]view plain copy////TextAndImageTextViewVC.m//YYTextDemo////Createdbylinpengon16/3/13.//Copyright©ibireme.Allrightsreserved.//#import"TextAndImageTextViewVC.h"#import"YYText.h"#import"UIView+YYAdd.h"#import"YYTextView.h"#import"YYImage.h"#import"NSBundle+YYAdd.h"#import"NSString+YYAdd.h"@interfaceTextAndImageTextViewVC()@endYYTextView*textView;@implementationTextAndImageTextViewVC-(void)viewDidLoad{[superviewDidLoad];[self.viewsetBackgroundColor:[UIColorwhiteColor]];textView=[[YYTextViewalloc]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=[NSMutableAttributedStringnew];UIFont*font=[UIFontsystemFontOfSize:16];//图片资源YYImage*image=[YYImageimageNamed:@"demo.jpg"];image.preloadAllAnimatedImageFrames=YES;//添加文本+图片[contentTextappendAttributedString:[[NSAttributedStringalloc]initWithString:@"这是第一站图片"attributes:nil]];{YYAnimatedImageView*imageView=[[YYAnimatedImageViewalloc]initWithImage:image];imageView.frame=CGRectMake(0,0,textView.width-10,textView.width/image.size.width*image.size.height);NSMutableAttributedString*attachText=[NSMutableAttributedStringyy_attachmentStringWithContent:imageViewcontentMode:UIViewContentModeScaleAspectFitattachmentSize:imageView.sizealignToFont:fontalignment:YYTextVerticalAlignmentCenter];[contentTextappendAttributedString:attachText];}//添加文本+图片[contentTextappendAttributedString:[[NSAttributedStringalloc]initWithString:@"\n接下来是第二张"attributes:nil]];{YYAnimatedImageView*imageView2=[[YYAnimatedImageViewalloc]initWithImage:image];imageView2.frame=CGRectMake(0,0,textView.width-10,textView.width/image.size.width*image.size.height);NSMutableAttributedString*attachText2=[NSMutableAttributedStringyy_attachmentStringWithContent:imageView2contentMode:UIViewContentModeScaleAspectFitattachmentSize:imageView2.sizealignToFont:fontalignment:YYTextVerticalAlignmentCenter];[contentTextappendAttributedString:attachText2];}textView.attributedText=contentText;[self.viewaddSubview:textView];//获取图片资源NSArray*attachments=textView.textLayout.attachments;for(YYTextAttachment*attachmentinattachments){YYAnimatedImageView*imageView=attachment.content;YYImage*image=(YYImage*)imageView.image;NSLog(@"获取到图片:%@",image);}NSArray*attachmentRanges=textView.textLayout.attachmentRanges;for(NSValue*rangeinattachmentRanges){NSRanger=[rangerangeValue];NSLog(@"资源所在位置:%ld长度:%ld",r.location,r.length);}}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];//Disposeofanyresourcesthatcanberecreated.}/*#pragmamark-Navigation//Inastoryboard-basedapplication,youwilloftenwanttodoalittlepreparationbeforenavigation-(void)prepareForSegue:(UIStoryboardSegue*)seguesender:(id)sender{//Getthenewviewcontrollerusing[seguedestinationViewController].//Passtheselectedobjecttothenewviewcontroller.}*/@end 以上代码加上注释应该很容易理解就不多讲了 注意 \n 这个换行符的使用

看效果图:

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。