为什么UIFont的lineHeight大于字体大小? [英] Why is lineHeight of UIFont larger than font size?

查看:67
本文介绍了为什么UIFont的lineHeight大于字体大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

But I found that font.lineHeight (log shows it is 17.900391) is larger than the font size which is set as 15.0.

日志消息显示:

高度17.900391,17.900391,数字行:1

height 17.900391, 17.900391, number lines:1

推荐答案

lineHeight =字体的升序(字母上方的一部分)
字体的前导(垂直间距)

例如适用于10号字体
A可能具有ascender = 10,descender = 0 = 0
g可能具有ascender = 5,descender = 4

lineHeight = a font's ascender (part of a letter that is above)
                  + a font's descender (so below baseline)
                  + a font's leading (vertical spacing)

e.g. for font size 10
A might have ascender=10, descender=0
g might have ascender=5, descender=4

lineHeight = 14 +前导... 14,5也许

lineHeight = 14 + leading ... 14,5 maybe

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

Note that the numbers here were random and only given to better illustrate the issue

这篇关于为什么UIFont的lineHeight大于字体大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆