美文网首页
使用YYLabel优化tableview和collectionv

使用YYLabel优化tableview和collectionv

作者: kwalter | 来源:发表于2018-09-29 11:10 被阅读50次

需求:

 在使用tableview 和 collectionview 展示商品信息的时候,需要显示富文本信息,由于cell 使用较多的富文本,导致在滚动的时候流畅度不是很理想

优化:

通过YYLabel 的异步渲染,大幅提升了滚动流畅性

实现:

我们在model类里面创建一个YYTextLayout属性,通过提前处理这个属性值,将需要展示的富文本信息缓存到model类中,当对Label 进行赋值时就可以直接赋值而不需要通过计算,减少cell刷新过程中的计算量,

YYLabel创建:

YYLabel *title = [YYLabel new];

title.displaysAsynchronously = YES; //开启异步渲染

title.ignoreCommonProperties = YES; //忽略属性

title.textLayout = model.titleLayout; //直接取 YYTextLayout

NSMutableAttributedString* string = [NSMutableAttributedString attachmentStringWithEmojiImage:IMAGE_NAMED(@"xjRedPicket") fontSize:11];

NSMutableAttributedString* attr = [[NSMutableAttributedString alloc] initWithString:F(@"¥%@+", self.price)];        

[attr appendAttributedString:string];        

[attr appendString:F(@" %@", self.consume)];        

[attr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:kFitWithWidth(15)] range:NSMakeRange(0, attr.length)];    

[attr addAttribute:NSForegroundColorAttributeName value:HEXCOLOR(0xFE3F56) range:NSMakeRange(0, attr.length)];                 

YYTextContainer* priceContainer = [YYTextContainer containerWithSize:CGSizeMake(KScreenWidth/2-kFitWithWidth(30), CGFLOAT_MAX)];        

_priceLayout = [YYTextLayout layoutWithContainer:priceContainer text:attr];

相关文章

网友评论

      本文标题:使用YYLabel优化tableview和collectionv

      本文链接:https://www.haomeiwen.com/subject/yzevoftx.html