Ascent
与Descent
的值为以baseLine
作为原点的坐标,根据这三个值,我们可以计算出字体的高度。
TextHeight = (Ascent - Descent) / EM-Square * TextSize
LineHeight = (Ascent - Descent + LineGap) / EM-Square * TextSize
上表中,我们已知宋体-常规的ascent
为1060
,descent
为-340
。
TextSize为100Pixcel的宋体常规字符高度为
height = (1060 - (-340)) / 1000 * 100 = 140px
所以对于宋体,1Px
的字高为1.4Px
。
常见字体LineGap
一般均为0
,所以一般lineHeight = textHeight
。
常用字体参数
iOS默认字体 - [San Francisco]
TextHeight = 1.193359375 TextSize
Android默认字体 - [Roboto - Regular]
TextHeight = 1.17187502 TextSize
UI适配误区
如上图Sketch
设计稿中,字体为28px
,字体居上下边框为32px
,如果按照这样的参数进行UI还原的话,以Android
默认设备为例,外围背景会比原来高28 * (1.17 - 1) = 4.76
个像素(Android IncludeFontPadding = false
)。
这是因为该设计稿中框选的lineHeight = textSize
,这在一般的字体中是不正确
的!会导致一些文字显示不下或者两行文字的上下端部分叠加。同理,用字的高度去得出TextSize
也是不正确
的!框选文字的时候不能刚刚够框选中文,实际上这种做法输入框输入个'j'
便会超出选框,虽然仍能显示。
正确做法应该将lineHeight
设置为 28 * 1.17 = 33
,然后再测出上下边距。
如图,文字的实际位置并没有变化,但是文字的lineHeight
变大了,上下边距相应减少为29px
与30px
。
对于设计稿中LineHeight > 字体实际高度
(如1.17 * textSize
)的情况下,我们可以设置lineSpace = lineHeight - 1.17 textSize
去精确还原行间距。
结论:UI中字体还原不到位一般是对字体高度理解有误解,实际上1Px
的字体在客户端中一般不等于1Px
,而等于1.19(iOS)
or 1.17 (Android)
个Px。
Android IncludeFontPadding
/**
- Set whether the TextView includes extra top and bottom padding to make
- room for accents that go above the normal ascent and descent.
- The default is true.
- @see #getIncludeFontPadding()
- @attr ref android.R.styleable#TextView_includeFontPadding
*/
public void setIncludeFontPadding(boolean includepad) {
if (mIncludePad != includepad) {
mIncludePad = includepad;
if (mLayout != null) {
nullLayouts();
requestLayout();
invalidate();
}
}
}
Android TextView
默认IncludeFontPadding
为开启状态,会在每一行字的上下方留出更多的空间。
if (getIncludeFontPadding()) {
fontMetricsTop = fontMetrics.top;
} else {
fontMetricsTop = fontMetrics.ascent;
}
if (getIncludeFontPadding()) {
fontMetricsBottom = fontMetrics.bottom;
} else {
fontMetricsBottom = fontMetrics.descent;
}
我们通过Textview
的源码可以发现,只有IncludeFontPadding = false
的情况下,textHeight
计算方式才与iOS
端与前端相统一。默认true
情况会选取top
与bottom
,这两个值在一般情况下会大于ascent
和descent
,但也不是绝对的,在一些字体中会小于ascent
和descent
。
public static class FontMetrics {
/**
- The maximum distance above the baseline for the tallest glyph in
- the font at a given text size.
/
public float top;
/* - The recommended distance above the baseline for singled spaced text.
/
public float ascent;
/* - The recommended distance below the baseline for singled spaced text.
/
public float descent;
/* - The maximum distance below the baseline for the lowest glyph in
- the font at a given text size.
/
public float bottom;
/* - The recommended additional space to add between lines of text.
*/
public float leading;
}
对于top
和bottom
,这两个值在 ttc/ttf
字体中并没有同名的属性,应该是Android
独有的名称。我们可以寻找获取FontMetrics
的方法(getFontMetrics
)进行溯源。
public float getFontMetrics(FontMetrics metrics) {
return nGetFontMetrics(mNativePaint, metrics);
}
@FastNative
private static native float nGetFontMetrics(long paintPtr, FontMetrics metrics);
Paint
的getFontMetrics
最终调用了native
方法nGetFontMetrics
,nGetFontMetrics
的实现在Android
源码中的Paint_Delegate.java类
@LayoutlibDelegate
/package/
static float nGetFontMetrics ( long nativePaint, long nativeTypeface,FontMetrics metrics){
// get the delegate
Paint_Delegate delegate = sManager.getDelegate(nativePaint);
if (delegate == null) {
return 0;
}
return delegate.getFontMetrics(metrics);
}
private float getFontMetrics (FontMetrics metrics){
if (mFonts.size() > 0) {
java.awt.FontMetrics javaMetrics = mFonts.get(0).mMetrics;
if (metrics != null) {
// Android expects negative ascent so we invert the value from Java.
metrics.top = -javaMetrics.getMaxAscent();
metrics.ascent = -javaMetrics.getAscent();
metrics.descent = javaMetrics.getDescent();
metrics.bottom = javaMetrics.getMaxDescent();
metrics.leading = javaMetrics.getLeading();
}
return javaMetrics.getHeight();
}
return 0;
}
由上可知top
和bottom
实际上取得是Java FontMetrics
中的MaxAscent
与MaxDescent
,对于MaxAscent
的取值OpenJDK官网论坛给出了答案:
Ideally JDK 1.2 should have used the OS/2 table value for usWinAscent,
or perhaps sTypoAscender (so there’s at least three choices here,
see http://www.microsoft.com/typography/otspec/recom.htm#tad for
more info).
For max ascent we could use the yMax field in the font header.
In most fonts I think this is equivalent to the value we retrieve from the hhea table,
hence the observation that both methods return the max ascent.
所以我们可以获知,Android
默认取的是字体的yMax
高度,通过查找Apple Font
手册我们可以知道yMax
是字符的边界框范围,所以我们可以得出以下公式:
includeFontPadding default true
TextHeight = (yMax - yMin) / EM-Square * TextSize
includeFontPadding false
TextHeight = (ascent - descent) / EM-Square * TextSize
Android
默认字体roboto
在默认includeFontPadding = true
情况下,textHeight = 1.32714844 textSize
。
所以Android UI
适配,如果不改变includeFontPadding
,可以将系数调整为1.327
总结
相同textSize
的字体,高度由字体本身决定
字体公式
TextHeight = (Ascent - Descent) / EM-Square * TextSize
LineHeight = (Ascent - Descent + LineGap) / EM-Square * TextSize
Android - includeFontPadding true
TextHeight = (yMax - yMin) / EM-Square * TextSize
客户端默认字体下,1个Px
的高度值并不为1Px
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
总结
写到这里也结束了,在文章最后放上一个小小的福利,以下为小编自己在学习过程中整理出的一个关于Flutter的学习思路及方向,从事互联网开发,最主要的是要学好技术,而学习技术是一条慢长而艰苦的道路,不能靠一时激情,也不是熬几天几夜就能学好的,必须养成平时努力学习的习惯,更加需要准确的学习方向达到有效的学习效果。
由于内容较多就只放上一个大概的大纲,需要更及详细的学习思维导图的
还有高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter全方面的Android进阶实践技术资料,并且还有技术大牛一起讨论交流解决问题。
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter全方面的Android进阶实践技术资料,并且还有技术大牛一起讨论交流解决问题。**
[外链图片转存中…(img-beGkyfnw-1713521922611)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!