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