#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);
}