百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

怼设计师系列:你知道 Android 的1px 多高?

bigegpt 2024-08-07 17:50 4 浏览

码个蛋(codeegg)第 593 次推文

在还原UI的时候我们常会发现一个问题,按照Sketch标注的尺寸去还原设计稿中的文字会产生几个Px的误差,字符上下有些许空白,以致于后期设计审查时频繁微调。

如上图为Android设备上100Px的不同字体显示的真实高度(includeFontPadding:false,下同),不同的字体的实际高度均不一致。

所以,为了精确还原我们需要了解1Px的字体到底有多高?

FontMetrics

在TrueType字体文件中,每一款字体文件都会定义一个em-square,它被存放于ttf文件中的'head'表中,一个em-square值可以为1000、1024或者2048等。

em-square相当于字体的一个基本容器,也是textSize缩放的相对单位。金属时代一个字符不能超过其所在的容器,但是在数字时代却没有这个限制,一个字符可以扩展到em-square之外,这也是设计一些字体时候挺方便的做法。

后续的ascent、descent以及lineGap等值都是相对于em-square的相对值。

ascent代表单个字符最高处至baseLine的推荐距离,descent代表单个字符最低处至baseLine的推荐距离。字符的高度一般由ascent和descent共同决定,对于em-square、ascent与descent我们可以通过FontTools解析字体文件获得。

FontTools

FootTools是一个完善易用的Python字体解析库,可以很方便地将TTX、TTF等文件转成文本编辑器打开的XML描述文件。

FontTools

安装

pip install fonttools

转码

ttx Songti.ttf

转码后会在当前目录生成一个Songti.ttx的文件,我们用文本编辑器打开并搜索'head'。

<head>
<!-- Most of this table will be recalculated by the compiler -->
<tableVersion value="1.0"/>
<fontRevision value="1.0"/>
<checkSumAdjustment value="0x7550297b"/>
<magicNumber value="0x5f0f3cf5"/>
<flags value="00000000 00001011"/>
<unitsPerEm value="1000"/>
<created value="Thu Nov 11 14:47:27 1999"/>
<modified value="Tue Nov 14 03:02:03 2017"/>
<xMin value="-99"/>
<yMin value="-150"/>
<xMax value="1032"/>
<yMax value="860"/>
<macStyle value="00000000 00000000"/>
<lowestRecPPEM value="12"/>
<fontDirectionHint value="1"/>
<indexToLocFormat value="1"/>
<glyphDataFormat value="0"/>
</head>

其中unitsPerEm便代表em-square,值为1000。 在Windows系统中,Ascent与Descent由'OS_2'表中的usWinAscent与usWinDescent决定。 但是在MacOS、iOS以及Android中,Ascent与Descent由'hhea'表中的ascent与descent决定。

<hhea>
<tableVersion value="0x00010000"/>
<ascent value="1060"/>
<descent value="-340"/>
<lineGap value="0"/>
<advanceWidthMax value="1000"/>
<minLeftSideBearing value="-99"/>
<minRightSideBearing value="-50"/>
<xMaxExtent value="1032"/>
<caretSlopeRise value="1"/>
<caretSlopeRun value="0"/>
<caretOffset value="0"/>
<reserved0 value="0"/>
<reserved1 value="0"/>
<reserved2 value="0"/>
<reserved3 value="0"/>
<metricDataFormat value="0"/>
<numberOfHMetrics value="1236"/>
</hhea>

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]

<unitsPerEm value="2048"/>
<ascent value="1950"/>
<descent value="-494"/>
<lineGap value="0"/>

TextHeight = 1.193359375 TextSize

Android默认字体 - [Roboto - Regular]

<unitsPerEm value="2048"/>
<ascent value="1900"/>
<descent value="-500"/>
<lineGap value="0"/>
<yMax value="2163"/>
<yMin value="-555"/>

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 != ) {
Layouts;
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 == ) {
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 != ) {
// 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 trueTextHeight = (yMax - yMin) / EM-Square * TextSize

includeFontPadding falseTextHeight = (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

iOS TextHeight = 1.193359375 TextSize
Android - IncludePadding : true TextHeight = 1.32714844 TextSize
Android - IncludePadding : false TextHeight = 1.17187502 TextSize

近期文章:

  • 社群升级:Max你的学习效率

  • 二进制 -- 你不知道的事

  • Python之父再度为996发声,而我们呢?

  • 程序员写墓志铭……

今日面试问题:

小伙伴们平时是怎么跟设计师沟通的?

在码个蛋(codeegg)后台回复「社群」即可加入学习群

相关推荐

VS Code上也能玩转Jupyter Notebook,这是一份完整教程

点击上方"码农真经"关注,星标或者置顶22点24分准时推送,第一时间送达来源:公众号机器之心|编辑:真经君码农真经(ID:coder_experience)第115次推文图源:...

看开发一款AR眼镜产品都需要那些东东?

开发AR眼镜应用时,使用的操作系统和编程语言主要取决于目标设备和开发平台。以下是主流AR眼镜的开发系统和语言总结:一、操作系统WindowsHolographic设备:微软HoloLens系列特点:...

ONNX Runtime 黑科技曝光:一行 C++ 代码让 ResNet 起飞,GitHub 已爆星

1.ONNXRuntime简介ONNXRuntime(ORT)是由微软开发的高性能推理引擎,支持跨平台(Windows/Linux/macOS)运行ONNX格式的深度学习模型。其核心优势...

海康工业相机SDK开发介绍

这篇文章,我来介绍一下海康工业相机SDK的使用方法。如果是老手,你可以跳过,如果是新手,可以看看,欢迎拍砖要使用海康的工业相机SDK,要先下载机器视觉工业相机SDKV3.2.0版本Runtime组...

深圳机器视觉相机USB3.0数字相机及千兆网口相机有什么特点?

四元数机器视觉相机的目的是将通过镜头投影到传感器的图像传送到能够储存、分析和(或者)显示的机器设备上。那么深圳机器视觉相机USB3.0数字相机及千兆网口相机有什么特点?相信不少人是有疑问的,今天深圳四...

蚁利智慧工地展厅控制系统:多媒体交互软件及中控系统开发方案

来源:蚁利科技以下是一篇关于隆兰智慧工地安全体验数字展厅多媒体互动软件及中控系统开发技术的技术方案框架,包含核心代码示例和实现思路:---#隆兰智慧工地安全体验数字展厅开发技术方案##一、系统架构...

C#程序员在工控行业到底有没有立足之地

当年忐忑的从互联网卷到工控圈时也一脸懵逼,结果发现咱C#程序员在工控界简直是万金油!今天就用我踩坑几年的经验给你们盘一盘转行路线。一、能撸哪些岗位?上位机开发工程师(工控圈入场券):天天和PLC、传感...

C#一行代码搞定OCR

本文将介绍如何使用C#代码来实现OCR功能。一、在VS中新建一个控制台应用,选择.net框架,.net6.0~8.0均可。创建好的项目如下图,然后在配置管理器中修改活动解决方案平台为x64,以及项目平...

在 C# WinForms 中 OpenCvSharp 进行边缘检测:Laplacian 算子介绍

边缘检测在计算机视觉和图像处理领域中具有非常重要的作用,它是物体识别、特征提取的基础。Laplacian算子是一种经典且常用的边缘检测方法,它对噪声非常敏感,能够很好地检测到图像中的细节和边缘。本文...

C# OpenCV机器视觉:对位贴合

在热闹非凡的手机维修街上,阿强开了一家小小的手机贴膜店。每天看着顾客们自己贴膜贴得歪歪扭扭,不是膜的边缘贴不整齐,就是里面充满了气泡,阿强心里就想:“要是我能有个自动贴膜的神器,那该多好啊,就可以让顾...

C# OpenCV机器视觉:缺陷检测

在一个阳光明媚的早晨,阿强正准备享受他的一杯咖啡,突然接到了老板的电话。“阿强,我们的生产线出现了问题!有几个产品的质量不合格,客户投诉不断!你能不能想办法解决这个问题?”阿强一听,心中一紧,随即灵光...

CHAPTER II 第二章

CHAPTERIICHAPTERII第二章Iresistedalltheway:anewthingforme...

万字图文,将&quot;类加载器&quot;与&quot;双亲委派机制&quot;一网打尽

引子大家想必都有过平时开发springboot项目的时候稍微改动一点代码,就得重启,就很烦网上一般介绍2种方式spring-boot-devtools,或者通过JRebel插件来实现"...

Java:Java中的微服务

  如果你想使用Java中的云原生微服务快速实现大规模可扩展性,那么不需要重新发明轮子。一些创新工具可以帮助你。通过Java培训课程,在Java(软件开发中最常用的编程语言)方面打下坚实的基础,更好地...

&quot;类加载器&quot;与&quot;双亲委派机制&quot;一网打尽

引子大家好,我是呼噜噜,大家想必都有过平时开发springboot项目的时候稍微改动一点代码,就得重启,就很烦网上一般介绍2种方式spring-boot-devtools,或者通过JRebel插件...