美文网首页
label计算文本高度

label计算文本高度

作者: 一米阳光k1992 | 来源:发表于2018-09-13 09:40 被阅读0次

//计算文本高度

+ (CGFloat)calculateStringHeight:(NSString *)string {

    CGFloat height = 0;

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

    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;

    NSDictionary * attributes = @{

                                  NSFontAttributeName:[FFStyle fontPingFangWithSize:14],

                                  NSParagraphStyleAttributeName: paragraphStyle

                                  };

    CGSize textRect = CGSizeMake(FF_SCREEN_WIDTH - 24, MAXFLOAT);

    CGFloat textHeight = [string boundingRectWithSize: textRect

                                              options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading

                                          attributes:attributes

                                              context:nil].size.height;

    height = ceil(textHeight);

    return height;

}

相关文章

网友评论

      本文标题:label计算文本高度

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