yytextview多种格式_YYText

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值