UILabel dynamic content inside UIScrollView

I have a UILabel (among other controls) stored in a UIScrollView. I want to dynamically populate a UILabel so that it automatically increases its height to fit its contents. I'm just wondering what the best thing is about resizing the UILabel, as well as ensuring that its parent UIScrollView also expands its scrollable area.

I tried setting UILabel # ofLines to 0, which, as I read, should automatically extend UILabel to match its contents, but I was not very lucky. Regarding recalculating the scrollable area of ​​UIScrollViews, I assume this is a case of calculating the height of its children after filling in the UILabel.

+3
source share
1 answer

After installing the new text in UILabel, call sizeToFiton it. You can then set the UIScrollView property contentSizeto the new UILabel frame size (and / or adjust the position of other views in the UIScrollView.

You may need to save the UIScrollView contentOffsetbefore setting it contentSize, and then revert contentOffsetto the saved value again .

+4
source

Source: https://habr.com/ru/post/1767766/


All Articles