Reputation: 75
When I run the following code (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);
The log output is:
Text 1 Size: 62.000000 17.000000
Text 2 Size: 300.000000 34.000000
Why are the heights different, here? In either case, it's one line of text; just one is truncated, the other is not.
Thanks!
Upvotes: 2
Views: 690
Reputation: 104092
The height is different because you gave it CGFLOAT_MAX as the height, so the method tries to figure out what size the text will be by breaking the text into multiple lines. In this case, apparently, two lines will be enough to contain that string in the width supplied.
Upvotes: 4