0

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

非常感谢!

4

1 回答 1

2

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

CGFloat maxHeight = label.font.lineHeight * 2;
CGSize constrainSize = CGSizeMake(285, maxHeight);
于 2013-03-09T18:44:51.663 回答