Steven
Steven

Reputation: 4963

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?

Upvotes: 3

Views: 5115

Answers (3)

jrturton
jrturton

Reputation: 119242

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.

Upvotes: 8

user537213
user537213

Reputation:

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.

Upvotes: 1

Midhun MP
Midhun MP

Reputation: 107121

If you don't want autolayout then disable it in the xib file.

If you want to change the contraint and it's value this tutorial will help you to deal with autolayout.

Upvotes: 0

Related Questions