Get UITextView dynamic height with auto layout after setting text

Piero picture Piero · Nov 21, 2014 · Viewed 44.7k times · Source

I have a UITextView not scrollable with auto layout set by interface builder, and the text increase or decrease dynamically with no problem, but i want know what is the new UITextView height after setting text, i'm trying to do this:

NSLog(@"text before: %.2f",self.myText.frame.size.height);
[self.myText setText:self.string];
NSLog(@"text after: %.2f",self.myText.frame.size.height);

this is the result:

text before: 47.50
text after: 47.50

the text is increased in the view when i run it, but the size is the same, how i can get the real height after setting text?

Answer

Pavel Gurov picture Pavel Gurov · Mar 23, 2016

All you have to do is:

  • set up all your constraints except the height one AND
  • set textView's scrollEnabled property to NO

The last part is what does the trick.

Your text view will size automatically depending on its text value.