次のコード (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
ここ、なんで高さが違うの?どちらの場合も、1 行のテキストです。1 つだけが切り捨てられ、もう 1 つは切り捨てられません。
ありがとう!