The UIKit additions to NSString include this method:
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
Invoke this method on the UITextView's text
property with the font you're using. As for the size parameter, the width
member should be the width of the TextView's frame, and the height
member should be the maximum height you will allow for the text view. Replace the frame of the UITextView with a new frame, containing its current position and the size returned by this method. For example:
CGSize newSize = [textView.text sizeWithFont:myFont constrainedToSize:CGSizeMake(textView.frame.size.x, /*insert desired maximum height as CGFloat*/)]
textView.frame = CGRectMake(textView.frame.point.x, textView.frame.point.y, newSize.width, newSize.height)
//You may also be able to do this, but I'm not sure and I can't test it right now
textView.frame.size = newSize;
UITextView inherits from UIScrollView, so I think this should only apply when you want to account for small amounts of text by decreasing the size of the TextView.