UILabel 中的行数
我需要获取 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最好将 UILabel 的文本布局视为黑盒(它可以做一些有趣的事情,例如当文本太长时自动调整字体大小)。
相反,请考虑使用 UIView 大小调整方法:
我注意到 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:
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.