gpt4 book ai didi

ios - 为什么 UIFont 的 lineHeight 大于字体大小?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:06:14 31 4
gpt4 key购买 nike

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

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的字体大小。

日志信息显示:

height 17.900391, 17.900391, number lines:1

最佳答案

lineHeight = 字体的上升部分(字母的一部分,在上面)
+ 字体的下降部分(因此低于基线)
+ 字体的前导(垂直间距)

例如字体大小 10
A 可能有 ascender=10,descender=0
g 可能有 ascender=5,descender=4

lineHeight = 14 + leading ... 14,5 也许


请注意,这里的数字是随机的,只是为了更好地说明问题

关于ios - 为什么 UIFont 的 lineHeight 大于字体大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33278645/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com