UIScrollView 内的动态 UILabel 内容
我有一个 UILabel (以及其他控件)存储在 UIScrollView 中。我想动态填充 UILabel,以便它自动扩展其高度以适应其内容。我只是想知道调整 UILabel 大小同时确保其父 UIScrollView 也将扩展其可滚动区域的最佳实践是什么。
我尝试将 UILabel 的行数设置为 0,我读过应该会自动扩展 UILabel 以适合其内容,但我运气不佳。在重新计算 UIScrollViews 可滚动区域方面,我想这是在填充 UILabel 后计算其子级高度的情况。
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 UILabel 中设置新文本后,对其调用
sizeToFit
。然后,您可以设置 UIScrollView 的contentSize
属性以匹配 UILabel 的新框架大小(和/或调整 UIScrollView 中其他视图的位置)。您可能需要存储 UIScrollView 的
contentOffset 在设置其
contentSize
之前,然后再次将contentOffset
设置回其保存的值。After setting the new text in the UILabel, call
sizeToFit
on it. You can then set the UIScrollView'scontentSize
property to match the UILabel's new frame size (and/or adjust the positions of the other views in the UIScrollView.You may need to store the UIScrollView's
contentOffset
prior to setting itscontentSize
and then set thecontentOffset
back to its saved value again.