UIScrollView中的动态UILabel内容 [英] Dynamic UILabel content inside of a UIScrollView
问题描述
我有一个UILabel(其他控件)存储在UIScrollView。我想动态填充UILabel,使其自动扩展其高度以适应其内容。我只是想知道有关调整UILabel的最佳做法是什么,同时确保其父UIScrollView也将扩大其可滚动区域。
I've got a UILabel (amongst other controls) stored within a UIScrollView. I want to dynamically populate the UILabel so that it automatically expands its height to fit its contents. I'm just wondering what the best practice is with regards to resizing the UILabel while also ensuring that its parent UIScrollView will also expand its scrollable area.
我试图设置UILabel的#行为0,我已经阅读应该自动展开UILabel适合其内容,但我没有太多的运气。在重新计算UIScrollViews可滚动区域,我想象是在填充UILabel之后计算其子元素的高度的情况。
I've tried to set the UILabel's # of Lines to 0, which I've read should automatically expand the UILabel to fit its contents, but I'm not having much luck. In terms of recalculating the UIScrollViews scrollable area, I imagine it's a case of calculating the height of its children after the UILabel is populated.
推荐答案
p>在UILabel中设置新文本后,调用 sizeToFit
。然后,您可以设置UIScrollView的 contentSize
属性以匹配UILabel的新框架大小(和/或调整UIScrollView中其他视图的位置)。
After setting the new text in the UILabel, call sizeToFit
on it. You can then set the UIScrollView's contentSize
property to match the UILabel's new frame size (and/or adjust the positions of the other views in the UIScrollView.
您可能需要在设置其 contentSize
之前存储UIScrollView的 contentOffset
contentOffset
返回到其已保存的值。
You may need to store the UIScrollView's contentOffset
prior to setting its contentSize
and then set the contentOffset
back to its saved value again.
这篇关于UIScrollView中的动态UILabel内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!