Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update UITextView maximumNumberOfLines programmatically and see update on UI.

Tags:

ios

uikit

swift

I have UITextView with disabled scrolling. I need to change maximumNumberOfLines programmatically and see updates on UI after this. UITextView uses auto layout.

textView.textContainer.maximumNumberOfLines = 0

This should make visible the whole text from the textView on UI.

P.S. Similar questions were asked many times but the answer that works for me I haven't found.

like image 399
Anessence Avatar asked Dec 23 '22 04:12

Anessence


1 Answers

I found out how to achieve it by myself. So basically when you change textView.textContainer.maximumNumberOfLines UIKit doesn't know about this (but I think should). Even when you try to update a layout. The solution is simple:

    textView.textContainer.maximumNumberOfLines = 0
    textView.invalidateIntrinsicContentSize() //add this line

Call this when something changes that affects the intrinsicContentSize to notice UIKit.

I hope it will be useful and save somebody's time.

like image 147
Anessence Avatar answered Dec 29 '22 11:12

Anessence