UITextView doesn't update its contentSize

情到浓时终转凉″ 提交于 2019-12-05 00:07:19
Dima

You can use sizeThatFits: to get the correct size.

CGFloat height = ceilf([textView sizeThatFits:textView.frame.size].height);

Here is a popular open source component that makes use of it to achieve dynamic resizing based on the textview content:   https://github.com/HansPinckaers/GrowingTextView

Swift version of the answer:

var newTextViewHeight = ceil(textView.sizeThatFits(textView.frame.size).height)
textView.frame.size.height = newTextViewHeight

This piece of code might also come in handy if you want to update the superview of the UITextView

textView.superview?.frame.size.height = textView.contentSize.height + 10
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!