需求:
?在使用tableview 和 collectionview 展示商品信息的時候,需要顯示富文本信息,由于cell 使用較多的富文本,導(dǎo)致在滾動的時候流暢度不是很理想
優(yōu)化:
通過YYLabel 的異步渲染,大幅提升了滾動流暢性
實現(xiàn):
我們在model類里面創(chuàng)建一個YYTextLayout屬性,通過提前處理這個屬性值,將需要展示的富文本信息緩存到model類中,當對Label 進行賦值時就可以直接賦值而不需要通過計算,減少cell刷新過程中的計算量,
YYLabel創(chuàng)建:
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];