yytextview多种格式_iOS YYText的使用笔记一(YYTextView图文编辑器)
YYText是強大的YYKit的一部分可以單獨下載
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來拼接文字和圖片(占位)
直接上代碼:
//
// TextAndImageTextViewVC.h
// YYTextDemo
//
// Created by linpeng on 16/3/13.
// Copyright ? 2016年 ibireme. All rights reserved.
//
#import
@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 ()
@end
YYTextView *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作者點個贊)
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的yytextview多种格式_iOS YYText的使用笔记一(YYTextView图文编辑器)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2016各银行白金卡申请标准盘点
- 下一篇: table每行自动触发ajax,tabl