Get UITextView dynamic height with auto layout after setting text
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
'sscrollEnabled
property toNO
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:
Set Content Compression Resistance Priority Vertical to 1000.
Lower the priority of constraint height for your UITextView. Just make it less than 1000.
In Attributes Inspector:
- 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.
Piero
Updated on July 23, 2021Comments
-
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 over 9 yearsJust tested, works for me. I put an example on my Github: github.com/guillaume-algis/SO-27060338
-
Nicolas Miari over 7 yearsThis doesn't work for me: The entered text is invisible, and after entering several lines it finally appears at the bottom!
-
Nicolas Miari over 7 yearsForget it: adding
textView.setContentOffset(CGPoint.zero, animated: false)
fixed it. -
Flimm over 7 yearsThis is a link-only answer, could you summarize the contents of the link in your answer?
-
Andrej about 7 yearsThat'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 over 6 yearsIt's isScrollEnabled now. Couldn't edit because it's too small of an edit
-
Alok over 6 yearsThanks for the suggession, I have updated my answer.
-
Roman Filippov over 6 yearsbut 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 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 over 5 yearsThis is the answer.
-
Abishek Thangaraj over 3 yearsThis 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 almost 3 yearsIt works perfectly, otherwise all the above codes doesn't work in my case. Thank you.
-
Abishek Thangaraj almost 3 yearsYes this method is working perfectly. I use this idea in coding design part.