Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get UITextView dynamic height with auto layout after setting text

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?

like image 399
Piero Avatar asked Nov 21 '14 11:11

Piero


2 Answers

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.

like image 191
Pavel Gurov Avatar answered Sep 28 '22 11:09

Pavel Gurov


If you prefer to do it all by auto layout:

In Size Inspector:

  1. Set Content Compression Resistance Priority Vertical to 1000.

  2. Lower the priority of constraint height for your UITextView. Just make it less than 1000.

enter image description here

In Attributes Inspector:

  1. Uncheck Scrolling Enabled.
like image 34
Michael Revlis Avatar answered Sep 28 '22 12:09

Michael Revlis