1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > YYText实现图文混排静态图和动态图同时显示

YYText实现图文混排静态图和动态图同时显示

时间:2020-10-31 01:39:43

相关推荐

YYText实现图文混排静态图和动态图同时显示

#import "YYText.h"

- (void)test{

NSString *content = @"[狂笑]的生日[微笑],清晨我放飞一群白鸽,给你带来一个,今天是你的生日我的祖国,清晨我放飞一群白鸽,给你带来一个,今天是你的生日我的祖国,清晨我放飞一群白鸽,给你带来一个,今天是你的生日我的祖国,清晨我放飞一群白鸽,给你带来一个,今天是你的生日我的祖国,清晨我放飞一群白鸽,给你带来一个,今天是你的生日我的祖国,清晨我放飞一群白鸽,给你带来一个,今天是你的生日我的祖国,清晨我放飞一群白鸽,给你带来一个,今天是你的生日我的祖国,清晨我放飞一群白鸽,给你带来一个";

NSMutableAttributedString *result = [[NSMutableAttributedString alloc] initWithString:content];

[result addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, content.length)];

[result addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0 , content.length)];

// 修改行间距

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];

[paragraphStyle setLineSpacing:4];

// //修改段落间距

// [paragraphStyle setParagraphSpacing:4];

//两端对齐

paragraphStyle.alignment = NSTextAlignmentJustified;

[result addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [content length])];

YYLabel *label = [YYLabel new];

label.frame = CGRectMake(0, 300, screenW, 80);

[self.view addSubview:label];

label.backgroundColor = [UIColor cyanColor];

label.numberOfLines = 0;

for (int i = 0; i < 2; i ++) {

NSString *cName = i == 0 ? @"1.gif":@"1.png";

NSString *path = [[NSBundle mainBundle]pathForResource:cName ofType:nil];

NSData *data = [NSData dataWithContentsOfFile:path];

YYImage *image = [YYImage imageWithData:data scale:2];

image.preloadAllAnimatedImageFrames = YES;

YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];

NSMutableAttributedString *attachText = [NSMutableAttributedString yy_attachmentStringWithContent:imageView contentMode:UIViewContentModeScaleAspectFill attachmentSize:CGSizeMake(20, 20) alignToFont:[UIFont systemFontOfSize:20] alignment:YYTextVerticalAlignmentCenter];

NSRange range = NSMakeRange(4 * i, 4);

[result replaceCharactersInRange:range withAttributedString:attachText];

}

label.attributedText = result;

// // 利用YYTextLayout计算高度

YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(Screen_Width, MAXFLOAT)];

YYTextLayout *textLayout = [YYTextLayout layoutWithContainer:container text: result];

//label.Ex_height = textLayout.textBoundingSize.height;

label.frame = CGRectMake(0, 300, screenW, textLayout.textBoundingSize.height);

NSLog(@"%f %f",textLayout.textBoundingSize.height,textLayout.textBoundingSize.width);

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。