如何垂直排列动态大小的 UILabels?
假设我有两个在 UITableViewCell
中垂直位于彼此下方的 UILabels
。两者的换行模式均设置为 UILineBreakModeWordWrap
。
它们的水平尺寸是固定的,但两者都可以根据它们显示的文本量垂直拉伸。如何动态定位下面的一个以使它们永远不会重叠?
Let's say I have two UILabels
positioned vertically below each other in a UITableViewCell
. The line break mode is set to UILineBreakModeWordWrap
for both.
Their horizontal size is fixed, but both can stretch vertically based on how much text they display. How do I position the one that's below dynamically so that they would never overlap?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我使用这个函数来获取文本的高度,然后将第二个标签的高度设置为结果。
然后像这样使用它:
然后使用“textSize”设置第二个标签高度。
希望它会有所帮助。
i use this function to get the height of the text, and then set the second label height to the result.
and you use it like that:
then use "textSize" to set the second label height.
hope it will help.
尝试 -[UILabel sizeThatFits:]。假设您将标签放在 300px 宽的列中,您可以传入 (300,99999) 的大小,它会告诉您实际需要适合该文本的大小。您可以使用它来调整适当地调整标签的框架。
Try -[UILabel sizeThatFits:]. Say you're laying the labels out in a 300px wide column, you can pass in a size of (300,99999), and it'll tell you the size it actually needs to fit that text in. You can use that to adjust the frames of your labels appropriately.
我通过重置下面视图的框架做了类似的事情(假设您已经完成了 sizeWithFont 或 sizeThatFits 并调整了标签大小)。与此类似:
I've done something similar by resetting the frame of the view below (assuming you've already done sizeWithFont or sizeThatFits and resized the labels). Similar to this: