Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set height constraint for UITextView

I'm fairly new to iOS development and am having a hard time sizing a UITextView to it's content. I've found several examples of calculating a strings render size and/or updating the frame size of a textview, however I can't seem to get those to work.

When I add a UITextView to my view it adds a Height constraint which I think is locking it to a specific height. How can I work around it, update the constraint via code, and/or get my UITextView to resize vertically to show my content?

like image 908
Steven Avatar asked Jul 19 '26 10:07

Steven


2 Answers

You can create an outlet to the vertical size constraint, and then adjust the .constant property of the constraint to amend the height of your text view.

like image 70
jrturton Avatar answered Jul 21 '26 01:07

jrturton


A UITextView inherits from UIScrollView. As part of it's logic, it changes the height of its contentSize property to allow the user to scroll vertically to any portion of the TextView's content.

I would recommend using the ContentSize property of your UITextView to figure out how "tall" your content really is. You can then set the frame of your UITextView accordingly.

OR: You could figure out the size of your text using the right method from the NSString+UIKitAdditions category.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!