iOS YYText的使用筆記一(YYTextView圖文編輯器)

來源:互聯網
上載者:User

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作者點個贊)










相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.