yytextview多种格式_iOS YYText的使用笔记一(YYTextView图文编辑器)
YYText是強(qiáng)大的YYKit的一部分可以單獨(dú)下載
Powerful text framework for iOS to display and edit rich text. (It's a component ofYYKit)
集成的時(shí)候建議使用cocopod管理
1.里面主要兩個(gè)控件:
YYTextView 和 YYLabel
現(xiàn)在主要是YYTextview的簡(jiǎn)單使用
YYText主要是NSMutableAttributedString來(lái)處理富文本 他的內(nèi)部實(shí)現(xiàn)可以自己去深究。
簡(jiǎn)單的圖文并排,使用NSMutableAttributedString創(chuàng)建一個(gè)對(duì)象 然后 appendAttributesString來(lái)拼接文字和圖片(占位)
直接上代碼:
//
// 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);
//創(chuàng)建最主要的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 接下來(lái)是第二張" 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 長(zhǎng)度: %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以上代碼加上注釋?xiě)?yīng)該很容易理解就不多講了 ?注意 \n 這個(gè)換行符的使用
看效果圖:
(可編輯的文本+圖片 有的app需要編輯文章功能 用這個(gè)就個(gè)可以大體實(shí)現(xiàn)了,圖片和文本都已經(jīng)獲取到了 到時(shí)后對(duì)應(yīng)傳到服務(wù)器,之前沒(méi)用YYTextview也實(shí)現(xiàn)過(guò)這種功能,效果比較差,用這個(gè)實(shí)現(xiàn),相當(dāng)完美,必須給YYText作者點(diǎn)個(gè)贊)
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的yytextview多种格式_iOS YYText的使用笔记一(YYTextView图文编辑器)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 2016各银行白金卡申请标准盘点
- 下一篇: table每行自动触发ajax,tabl