I have a UITextView
called recipeDesc
. I would like to have an option within my app to dynamically adjust the height of the UITextView
to fit the content (text) inside the UITextView
(so I need to know how to do it programmatically). I found some answers for Objective-C, but I am not very familiar with it, so I don't know how to implement it in Swift.
Thanks in advance for any help.
You can get the ideal content size for your UITextView
simply with:
let contentSize = textView.sizeThatFits(textView.bounds.size)
So after that, you can adjust it's frame with that height:
var frame = textView.frame
frame.size.height = contentSize.height
textView.frame = frame
(I originally had used textView.contentSize.height
but that does not work when the view has not rendered yet. For example in viewDidLoad
)
If you use Auto Layout, the rules are a bit different. Update your question if you do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With