iphone - numberOfLines=2 的 UILabel 使用什么约束高度? (使用 [NSString sizeWithFont...])

标签 iphone ios uiview nsstring uilabel

对于行数有限的 UILabel,如何确定在 [NSString sizeWithFont:constrainedToSize:...] 中使用的 constrainedToSize.height?

下面的约束高度是无限的(MAXFLOAT),但是当限制为 X 行时标签的最大高度如何使用?

UILabel * label = [[UILabel alloc] init];
label.numberOfLines = 2;
label.text = @"Some really long text";

// what to use instead of MAXFLOAT?
CGSize constrainSize = CGSizeMake(285, MAXFLOAT);

CGSize size = [label.text
               sizeWithFont: [UIFont boldSystemFontOfSize:17.0]
               constrainedToSize:constrainSize
               lineBreakMode:NSLineBreakByWordWrapping
               ];

非常感谢!

最佳答案

如果您已经知道您希望标签为 2 行,则使用:

CGFloat maxHeight = label.font.lineHeight * 2;
CGSize constrainSize = CGSizeMake(285, maxHeight);

关于iphone - numberOfLines=2 的 UILabel 使用什么约束高度? (使用 [NSString sizeWithFont...]),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15314424/

相关文章:

iPhone 应用程序在解析 XML 时卡住

iphone - iOS:是否有像主屏幕一样的开源启动器 View ?

iphone - 自动加载 UITableViewController 的 XIB

ios - 获取当前可见屏幕的中间 Y 值

ios - 在ios 7中制作透明导航栏和透明标签栏。

php - quincykit 无法符号化

ios - IOS中的日期时间转换

ios - `UITabBarController` 切换标签时去除黑色背景

Swift UIView 子类初始化失败

ios - 停止 UIView 中的所有异步任务