Take the 2-minute tour ×

I have several UITextViews that contain numbers between one and three digits. I'd like these views to expand and shrink as the number becomes larger or smaller. At the moment the text is shrinking and the TextViews size remains the same.

I'd preferable like to do this using the xib file (I'm very surprised this isn't a meagre check box) but a coded answer would be great if this isn't possible.

share|improve this question
Did you check NSString -sizeWithFont: ? It must be somewhere at top search results when you are looking for iOs NSString size. –  A-Live Oct 24 '12 at 20:30
I'll try that search, I've been searching for expandable UITextView and wrap TextView round text and so on. I assumed this would be a property of the UITextView and not the String within it. Thanks –  Deco Oct 24 '12 at 20:31
AFAIK there's no such property of the text view, you can find the reference docs of UITextView at developer.apple.com to see everything at one place. What I tried to tell you is that you can check the text size when the delegate text-changed event is fired, then when you have the text size you can change the textView frame as you see it needed. –  A-Live Oct 25 '12 at 10:13
add comment

1 Answer

up vote 2 down vote accepted

I'd recommend using the contentSize property of the UITextView:

   self.textView.frame = CGRectMake(self.textView.frame.origin.x, self.textView.frame.origin.y, self.textView.contentSize.width, self.textView.contentSize.height);
share|improve this answer
I threw this in to my viewDidLoad method but I'm getting a sigabrt runtime error. for(UITextView *statPanel in statPanels) { statPanel.frame = CGRectMake(statPanel.frame.origin.x, statPanel.frame.origin.y, statPanel.contentSize.width, statPanel.contentSize.height); } –  Deco Oct 24 '12 at 21:15
add comment

Your Answer


