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