简体   繁体   English

iOS自定义字体在不同语言中的垂直对齐

[英]Vertical alignment of iOS custom font in different languages

I am using a custom .ttf font called Classic Robot in my iOS app. 我在iOS应用程序中使用了称为Classic Robot的自定义.ttf字体。 I am trying to add Japanese translation and this font appears to not support Japanese characters. 我正在尝试添加日语翻译,并且该字体似乎不支持日语字符。 This is not a problem initially because it appears iOS automatically changes the font to the system default font which can support Japanese. 最初这不是问题,因为iOS似乎自动将字体更改为可以支持日语的系统默认字体。

My issue is that these two fonts have different vertical alignment when rendered by iOS as in the below pictures. 我的问题是,当由iOS渲染时,这两种字体具有不同的垂直对齐方式,如下图所示。 As you can see, the Japanese font is aligned near the top of the white box (which is the frame) whereas the English font sits somewhere near the middle. 如您所见,日语字体在白色框(即框架)顶部附近对齐,而英语字体则在中间位置附近。 This makes it difficult for me to layout text properly. 这使我很难正确布局文本。 Does anyone know why this might be the case? 有谁知道为什么会这样?

在此处输入图片说明在此处输入图片说明

I believe it might have to do with the ascender and descender properties on the iOS font I cannot be certain. 我相信这可能与我不确定的iOS字体上的ascender属性和下降属性有关。

Your question is: "Does anyone know why this might be the case?" 您的问题是:“有人知道为什么会这样吗?” The answer is that the ascender property of the Japanese font is smaller than the Classic Robot one. 答案是日语字体的ascender属性小于经典机器人字体的ascender属性。 This can be fixed by generating a custom Japanese font that has an ascender property large enough to make it match the spacing you get from the other font. 可以通过生成具有升序属性的自定义日语字体来解决此问题,该字体具有足够的ascender属性,以使其与您从其他字体获得的间距匹配。 This can be done by downloading the Apple Font Tool Suite and following the instructions posted in this answer . 可以通过下载Apple字体工具套件并按照此答案中的说明进行操作。

Also for buttons you can solve the issue by increasing the insets: 同样对于按钮,您可以通过增加插入量来解决问题:

myButton.contentEdgeInsets = UIEdgeInsetsMake(15.0, 0.0, 0.0, 0.0);

You can specify a different value for each of the four insets (top, left, bottom, right). 您可以为四个插图(上,左,下,右)中的每一个指定不同的值。 A positive value shrinks, or insets, that edge—moving it closer to the center of the button. 正值会缩小或插入该边缘,将其移近按钮中心。 A negative value expands, or outsets, that edge. 负值扩展或起始于该边缘。 This works for the button image and button title. 这适用于按钮图像和按钮标题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM