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

Viewed 772

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.

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.

Related