//计算字符串高度
- (CGSize)labelAutoCalculateRectWith:(NSString*)text FontSize:(CGFloat)fontSize MaxSize:(CGSize)maxSize
{
NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc]init];
paragraphStyle.lineBreakMode=NSLineBreakByWordWrapping;
NSDictionary* attributes =@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize],NSParagraphStyleAttributeName:paragraphStyle.copy};
CGSize labelSize = [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
labelSize.height=ceil(labelSize.height);
labelSize.width=ceil(labelSize.width);
return labelSize;
}
示例:
[self labelAutoCalculateRectWith:str FontSize:14 MaxSize:CGSizeMake(270, 2000)];
原文:http://www.cnblogs.com/huangzs/p/4541067.html