计算富文本高度不准的坑

+ (NSAttributedString *)getDescribleContentWithContent:(NSString *)content rateModel:(SHGenerateReportModels *)model{

    

    NSMutableDictionary *attributesDict = [NSMutableDictionary dictionary];

    [attributesDict setValue:[UIFont systemFontOfSize:18.0] forKey:NSFontAttributeName];

    [attributesDict setValue:[UIColor colorWithHexString:@"#332532"] forKey:NSForegroundColorAttributeName];

    NSMutableParagraphStyle *muParagraph = [[NSMutableParagraphStyle alloc] init];

    // 记得设置段落的相关属性

    muParagraph.lineBreakMode = NSLineBreakByWordWrapping;

    muParagraph.alignment = NSTextAlignmentLeft;

    muParagraph.lineSpacing = 10; // 行距

    [attributesDict setValue:muParagraph forKey:NSParagraphStyleAttributeName];

    NSMutableAttributedString *describeContent = [[NSMutableAttributedString alloc] initWithString:content attributes:attributesDict];

    

    CGFloat width = [UIScreen mainScreen].bounds.size.width - 96.0;

    CGFloat height = [describeContent boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height;

    // 向上取整 + 1.;

    model.describleLabelHeight = ceil(height) + 1.0;

    

    return describeContent;

}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容