原文: http://www.cnblogs.com/A--G/p/4819189.html
iOS 2.0之后 sizeWithFont就被弃用了:
//计算textview 高度
- (float) heightForTextView: (UITextView *)textView WithText: (NSString *) strText{ // float fPadding = 16.0; // 8.0px x 2
// CGSize constraint = CGSizeMake(textView.contentSize.width - fPadding, CGFLOAT_MAX);
//
// CGSize size = [strText sizeWithFont: textView.font constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping]; // float fHeight = size.height + 16.0;
可以采用boundingRectWithSize,这里举个栗子~
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
NSDictionary *attributes = @{NSFontAttributeName:textView.font, NSParagraphStyleAttributeName:paragraphStyle.copy}; float fPadding = 16.0; // 8.0px x 2
CGSize constraint = CGSizeMake(textView.contentSize.width - fPadding, CGFLOAT_MAX); CGSize size = [strText boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size; float fHeight = size.height + 16.0; return fHeight;