当我运行以下代码(iOS 6.1 SDK)时:
UIFont *font = [UIFont fontWithName:@"Avenir" size:12.0];
NSString *text1 = @"Short Label";
NSString *text2 = @"Long Label Whose Text Will Be Truncated Because It Is Too Long For The View";
CGSize text1Size = [text1 sizeWithFont:font
constrainedToSize:
CGSizeMake(self.view.frame.size.width, CGFLOAT_MAX)
lineBreakMode:NSLineBreakByTruncatingTail];
CGSize text2Size = [text2 sizeWithFont:font
constrainedToSize:
CGSizeMake(self.view.frame.size.width, CGFLOAT_MAX)
lineBreakMode:NSLineBreakByTruncatingTail];
NSLog(@"Text 1 Size: %f %f", text1Size.width, text1Size.height);
NSLog(@"Text 2 Size: %f %f", text2Size.width, text2Size.height);
日志输出为:
Text 1 Size: 62.000000 17.000000
Text 2 Size: 300.000000 34.000000
为什么这里的高度不同?无论哪种情况,它都是一行文本;只有一个被截断,另一个没有。
谢谢!