Get UITextView dynamic height with auto layout after setting text

47,631

Solution 1

This should work:

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

Here's an example implementation on my Github: https://github.com/guillaume-algis/SO-27060338

Solution 2

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.

Solution 3

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.

Solution 4

I have used the code given on following link AutoLayout with Dynamic UITextView height and it worked for me :)

Solution 5

Swift 3.0

textView.isScrollEnabled = false

This allow AutoLayout to do its job.

Share:
47,631
Piero
Author by

Piero

Updated on July 23, 2021

Comments

  • Piero
    Piero almost 3 years

    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?

  • Guillaume Algis
    Guillaume Algis over 9 years
    Just tested, works for me. I put an example on my Github: github.com/guillaume-algis/SO-27060338
  • Nicolas Miari
    Nicolas Miari over 7 years
    This doesn't work for me: The entered text is invisible, and after entering several lines it finally appears at the bottom!
  • Nicolas Miari
    Nicolas Miari over 7 years
    Forget it: adding textView.setContentOffset(CGPoint.zero, animated: false) fixed it.
  • Flimm
    Flimm over 7 years
    This is a link-only answer, could you summarize the contents of the link in your answer?
  • Andrej
    Andrej about 7 years
    That's essentially the only thing that's needed. I've added the textView in Interface builder, so I had to uncheck the "Scrolling Enabled" in IB in identity inspector of the UITextView. Also, the only constraints I've added were the top, leading and trailing. So that the height was not defined by constraints, those allowing the size to change automatically.
  • Plot
    Plot over 6 years
    It's isScrollEnabled now. Couldn't edit because it's too small of an edit
  • Alok
    Alok over 6 years
    Thanks for the suggession, I have updated my answer.
  • Roman Filippov
    Roman Filippov over 6 years
    but when you typing you should see end of the text. scroll to bottom with next line of code: textView.setContentOffset(CGPoint(x: 0, y: textHeight - textView.bounds.size.height), animated: true)
  • Hlung
    Hlung over 6 years
    @RomanFilippov It's a bit trickier than that though. You could be moving the text input cursor to the top and and add new text there. Which means new text can be added anywhere. I guess one missing piece is detecting where the text is inserted and scroll to there instead, which can be top, or bottom, or somewhere in the center.
  • Daniel Wood
    Daniel Wood over 5 years
    This is the answer.
  • Abishek Thangaraj
    Abishek Thangaraj over 3 years
    This is working for my message designing. but I need only increase for four lines after the four lin. textView stop the height increase and scroll. anyone knowns please answer or share the link.
  • Fredric Cliver
    Fredric Cliver almost 3 years
    It works perfectly, otherwise all the above codes doesn't work in my case. Thank you.
  • Abishek Thangaraj
    Abishek Thangaraj almost 3 years
    Yes this method is working perfectly. I use this idea in coding design part.