为什么UIFont的lineHeight比字体大?

时间:2015-10-22 10:17:12

标签: ios fonts

我使用下面的代码来计算宽度有限的字符串行数。

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);

但我发现font.lineHeight(日志显示为17.900391)大于设置为15.0的字体大小。

日志消息显示:

  

身高17.900391,17.900391,数字行:1

1 个答案:

答案 0 :(得分:7)

lineHeight =字体的上升(上方字母的一部分)
+字体的下行(低于基线)
+字体的前导(垂直间距)

e.g。字体大小10
A可能有ascender = 10,descender = 0
g可能有ascender = 5,descender = 4

lineHeight = 14 +领先...... 14,5也许

请注意,此处的数字是随机的,仅用于更好地说明问题