views:

61

answers:

1

I created a nib for a specific view I have. The view has a text field that may change height depending on the amount of text in the view's "represented object". For example, a blog post screen would have to handle different amounts of text as blog posts are not the same length and you obviously only want one nib to represent all blog posts.

Here is a screen shot of my nib settings. Do you know what is wrong? I am pretty sure it is just staying at the height I give it.

Thanks!

+1  A: 

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.

Endemic