+ (CGSize)contentSizeForContent:(NSString *)content withFixedWidth:(CGFloat)width {
CGSize maxSize = CGSizeMake(width, MAXFLOAT);
UIFont *font = [UIFont systemFontOfSize:14.0];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentLeft;
CGRect suggestRect = [content boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: font, NSParagraphStyleAttributeName: paragraphStyle} context:nil];
CGSize resultSize = CGSizeMake(width, ceil(suggestRect.size.height));
return resultSize;
}
如上代码,需要注意算完高度需要用ceil
来处理一下做向上取整。
更多:iOS面试题合集