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 *)imageV