UIScrollView 内的动态 UILabel 内容

发布于 2024-09-26 01:56:49 字数 257 浏览 9 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

待天淡蓝洁白时 2024-10-03 01:56:49

在 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's contentSize 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 its contentSize and then set the contentOffset back to its saved value again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文