对于行数有限的 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/