遇到一个很奇怪的问题,一个tableivewcell 消失不见了,
打开图层,是空的,没有隐藏,也没有移除,后来经过调试发现
是一个生成富文本的地方导致的
- (NSAttributedString *)returnFontColorAttributeString:(NSString *)string WithLineHeight:(CGFloat) lineHeight Font:(UIFont *)font Color:(NSString *)defaultColor lineBreakMode:(NSLineBreakMode)lineBreakMode {
if (isBlankString(string)) {
return nil;
}
NSString *preString = [NSString stringWithFormat:@"<span style=\"color:#%@; font-size:12\">",defaultColor];
NSString *suffixString = @"</span>";
NSString *newString = [[preString stringByAppendingString:string] stringByAppendingString:suffixString];
NSDictionary *dic = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[newString dataUsingEncoding:NSUnicodeStringEncoding] options:dic documentAttributes:nil error:nil];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.maximumLineHeight = lineHeight;
paragraphStyle.minimumLineHeight = lineHeight;
paragraphStyle.alignment = NSTextAlignmentCenter;
paragraphStyle.paragraphSpacing = 1;
paragraphStyle.lineBreakMode = lineBreakMode;
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
paragraphStyle,NSParagraphStyleAttributeName,
@0.0f,NSBaselineOffsetAttributeName,
font,NSFontAttributeName,
nil];
[attrStr addAttributes:attributes range:NSMakeRange(0, attrStr.length)];
return attrStr;
}
因为要生成大量的html富文本,内存消耗过大,导致展示不出来,解决方法,添加异步创建富文本
修复调用的时候这样调用
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSAttributedString * attribute = [self returnFontColorAttributeString:userModel.nickname WithLineHeight:17*PLUS_SCALE Font:appFont(14*PLUS_SCALE, NO) Color:@"333333" lineBreakMode:NSLineBreakByTruncatingTail];
dispatch_async(dispatch_get_main_queue(), ^{
self.nameLabel.attributedText = attribute;
});
});