I have a UITextView like so:
var textView = UITextView(frame: CGRectMake(0,0,200,20))
textView.scrollEnabled = true
textView.editable = true
self.view.addSubview(textView)
Nothing weird there
Then I want to update the text:
textView.text = "some short text"
If I request the .contentSize.height for it I get 20
which is correct
If the text is longer and has more lines:
textView.text = "some longer text\n another line\n one more line"
And get the height again I get 75
which is also correct.
But as soon as I make it not editable and not scrollable it fails and always returns 20
textView.scrollEnabled = false
textView.editable = false
textView.text = "some short text"
print(textView.contentSize.height) //prints 20
textView.text = "some longer text\n another line\n one more line"
print(textView.contentSize.height) //prints 20
Is there something I'm doing wrong here?
If I'm not wrong, you can disable scroll then you set contentSize.height
equal to view.height
.
In second i don't know reason its behavior, but if you modify your code like:
textView.editable = true
textView.text = "some short text"
print(textView.contentSize.height) //prints 20
textView.text = "some longer text\n another line\n one more line"
print(textView.contentSize.height) //prints 20
extView.editable = false
you get right content height. I think so - then you set editable to false, you also disable some option who will change content size
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