Reputation: 6952
I use the code below to calculate the number of lines of string with limited width.
NSString *text = @"abcdefgh";
UIFont *font = [UIFont systemFontOfSize:15.0];
CGFloat h = [text suggestHeightWithFont:font width:320.0];
NSInteger lines = (h > font.lineHeight) ? h/font.lineHeight+1 : h/font.lineHeight;
NSLog(@"height %f, %f, number lines:%ld", h, font.lineHeight, (unsigned long)lines);
But I found that font.lineHeight
(log shows it is 17.900391) is larger than the font size which is set as 15.0
.
The log message shows:
height 17.900391, 17.900391, number lines:1
Upvotes: 7
Views: 5553
Reputation: 50109
e.g. for font size 10
A might have ascender=10, descender=0
g might have ascender=5, descender=4
lineHeight = 14 + leading ... 14,5 maybe
Note that the numbers here were random and only given to better illustrate the issue
Upvotes: 15