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

该博客介绍了如何使用YYText库在iOS应用中创建一个带有动态和静态图的图文混排效果。通过设置字体颜色、大小、行间距以及添加YYAnimatedImageView来插入动图,实现了文本和图片的完美结合。

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

}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值