Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why UITextView.contentSize.height doesn't work if it's not editable or scrollEnabled in Swift?

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?

like image 332
matt Avatar asked Jan 09 '16 10:01

matt


1 Answers

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

like image 179
Citrael Avatar answered Sep 30 '22 12:09

Citrael