UILabel 中的行数

发布于 2024-09-24 23:43:32 字数 346 浏览 0 评论 0原文

我需要获取 UILabel 中的行数,以便我可以将另一个 UILabel 移动到最后一行文本的底部。 我已经做到了:

int lines = [cellDetailTextLabel.text sizeWithFont:cellDetailTextLabel.font constrainedToSize:cellDetailTextLabel.frame.size lineBreakMode:UILineBreakModeWordWrap].height /16;

但它并不完美,因为在某些情况下,UILabel 的长度为 3 行,但上面的代码只返回 2,但并非所有 3 行 UILabel 都是这种情况。

I need to get the number of lines in a UILabel, so that I can move another UILabel to be at the bottom of the last line of text.
I have done:

int lines = [cellDetailTextLabel.text sizeWithFont:cellDetailTextLabel.font constrainedToSize:cellDetailTextLabel.frame.size lineBreakMode:UILineBreakModeWordWrap].height /16;

but well it's not perfect because there are some cases when the UILabel is 3 lines long but the above code only returns 2, but this is not the case for all 3 lines of UILabels.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

墨小沫ゞ 2024-10-01 23:43:32

最好将 UILabel 的文本布局视为黑盒(它可以做一些有趣的事情,例如当文本太长时自动调整字体大小)。

相反,请考虑使用 UIView 大小调整方法:

// Ask the label to shrink (or grow) until it fits its text:
[cellDetailTextLabel sizeToFit];
// Get the frame.
CGRect r = cellDetailTextLabel.frame;
// Move its origin to below cellDetailTextLabel
r.origin.y = CGRectGetMaxY(r);
// Set its size to the size of the second label
r.size = cellLabel2.frame.size;
// Finally, move the second label
cellLabel2.frame = r;

我注意到 iPhone 4 上的文本大小调整有一些奇怪的行为(有时标签会出现太高的额外行);我不确定这个问题是否在 4.1 中得到修复。

It's better to treat UILabel's text layout as a black box (it does fun things like auto-adjusting font size when the text gets too long).

Instead, consider using UIView sizing methods:

// Ask the label to shrink (or grow) until it fits its text:
[cellDetailTextLabel sizeToFit];
// Get the frame.
CGRect r = cellDetailTextLabel.frame;
// Move its origin to below cellDetailTextLabel
r.origin.y = CGRectGetMaxY(r);
// Set its size to the size of the second label
r.size = cellLabel2.frame.size;
// Finally, move the second label
cellLabel2.frame = r;

I've noticed some odd behaviour with text-sizing on iPhone 4 (occasionally labels are an extra line too high or so); I'm not sure if this is fixed in 4.1.

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