label计算文本高度

2018-09-13  本文已影响0人  一米阳光k1992

//计算文本高度

+ (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;

}

上一篇下一篇

猜你喜欢

热点阅读