如何垂直排列动态大小的 UILabels?

发布于 2024-10-07 14:59:47 字数 190 浏览 0 评论 0原文

假设我有两个在 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 技术交流群。

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

发布评论

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

评论(3

呆萌少年 2024-10-14 14:59:47

我使用这个函数来获取文本的高度,然后将第二个标签的高度设置为结果。

- (CGFloat)HeightOfText:(NSString *)textToMesure widthOfLabel:(CGFloat)width
{
    UIFont *textFont = [UIFont systemFontOfSize:16];
    CGSize ts = [textToMesure sizeWithFont:textFont constrainedToSize:CGSizeMake(width-20.0, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap];
    return ts.height+25.0; //you can change the last number to fit the space you wish to have between the labels.
}

然后像这样使用它:

NSString *firstLabelText = @"the text";
CGFloat textSize = [self HeightOfText:firstLabelText widthOfLabel:firstLabel.frame.size.width];

然后使用“textSize”设置第二个标签高度。

希望它会有所帮助。

i use this function to get the height of the text, and then set the second label height to the result.

- (CGFloat)HeightOfText:(NSString *)textToMesure widthOfLabel:(CGFloat)width
{
    UIFont *textFont = [UIFont systemFontOfSize:16];
    CGSize ts = [textToMesure sizeWithFont:textFont constrainedToSize:CGSizeMake(width-20.0, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap];
    return ts.height+25.0; //you can change the last number to fit the space you wish to have between the labels.
}

and you use it like that:

NSString *firstLabelText = @"the text";
CGFloat textSize = [self HeightOfText:firstLabelText widthOfLabel:firstLabel.frame.size.width];

then use "textSize" to set the second label height.

hope it will help.

妄想挽回 2024-10-14 14:59:47

尝试 -[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.

夕色琉璃 2024-10-14 14:59:47

我通过重置下面视图的框架做了类似的事情(假设您已经完成了 sizeWithFont 或 sizeThatFits 并调整了标签大小)。与此类似:

- (void)positionView:(UIView *)aView belowView:(UIView *)anotherView withPadding:(int)padding
{
   CGRect aFrame = aView.frame;
   if (anotherView)
   {
      aFrame = (anotherView.frame.origin.y + anotherView.frame.size.height);
   }
   aFrame.origin.y += padding;
   aView.frame = aFrame;
}

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:

- (void)positionView:(UIView *)aView belowView:(UIView *)anotherView withPadding:(int)padding
{
   CGRect aFrame = aView.frame;
   if (anotherView)
   {
      aFrame = (anotherView.frame.origin.y + anotherView.frame.size.height);
   }
   aFrame.origin.y += padding;
   aView.frame = aFrame;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文