计算富文本高度不准的坑

2020-04-21  本文已影响0人  扶兮摇兮
+ (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;

}
上一篇 下一篇

猜你喜欢

热点阅读