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
原创粉丝点击