IOS计算文字高度

1.计算文字长度

   NSString* str = @"你好";

 const float kFontSize = 18.f;

    NSStringDrawingOptions options =  NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;

    CGRect rect = [str boundingRectWithSize:CGSizeMake(MAXFLOAT,MAXFLOAT) options:options attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kFontSize]} context:nil];

    CGFloat realHeight = ceilf(rect.size.height);

2.如果限制文字的宽度或高度,则将CGSizeMake(MAXFLOAT,MAXFLOAT)中的相应参数修改成具体的值

原文地址:https://www.cnblogs.com/guoxiaoqian/p/4658984.html