Свифт
public static func requiredHeightForLabel(text : String, font : UIFont, width : CGFloat) -> CGFloat {
let label:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: .greatestFiniteMagnitude))//UILabel(frame: CGRectMake(0, 0, width, CGFloat.max))
label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping
label.font = font
label.text = text
label.sizeToFit()
return label.frame.height
}
объектив c
+(CGFloat)getLabelHeightForString:(NSString *)string font:(UIFont *)font
{
CGSize size = CGSizeMake(lablwidth, MAXFLOAT);
NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
CGSize boundingBox = [string boundingRectWithSize:size
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:font}
context:context].size;
size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
return size.width;
}