自定义安装的字体未在 UILabel 中正确显示 [英] Custom installed font not displayed correctly in UILabel

查看:32
本文介绍了自定义安装的字体未在 UILabel 中正确显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用从 Adob​​e Font Collection Pro Package 获得的 Helvetica Neue Condensed 字体.不幸的是,当我在 UILabel 中使用它时,它似乎绘制不正确.

行高似乎计算正确(我认为),但是当显示字体时,它与边界框的最顶部对齐.我调用了 [myLabel sizeToFit] 并且只调整了宽度以生成此屏幕截图:

我对字体的粗体和常规版本都遇到了同样的问题.我能够从 OSX 中提取一个 Helvetica Neue Bold 版本并将其放在我的设备上,它显示正常(上图中的绿色背景).

字体文件或我的代码有什么问题会导致它以这种方式绘制?

解决方案

我在这里发布了一个涉及修补 ttf 字体文件的解决方案

一个>:

这是适用于我的自定义字体的解决方案,它在 UILabel、UIButton 等中存在相同的问题.结果证明,字体的问题在于,与系统字体的值相比,它的 Ascender 属性太小了.Ascender 是字体字符上方的垂直空白.要修复您的字体,您必须下载 Apple Font Tool Suite 命令行实用程序.然后使用您的字体并执行以下操作:

~$ ftxdumperfuser -t hhea -A d Bold.ttf

这将创建Bold.hhea.xml.用文本编辑器打开它并增加 ascender 属性的值.您将不得不进行一些试验,以找出最适合您的确切值.就我而言,我将其从 750 更改为 1200.然后使用以下命令行再次运行该实用程序以将您的更改合并回 ttf 文件:

~$ ftxdumperfuser -t hhea -A f Bold.ttf

然后在您的应用中使用生成的 ttf 字体.

I'm trying to use a Helvetica Neue Condensed font which I got from the Adobe Font Collection Pro Package. Unfortunately, it seems to draw incorrectly when I use it within a UILabel.

The line height seems to be calculated correctly (I think), but when the font is displayed, it is aligned to the very top of the bounding box. I called [myLabel sizeToFit] and only adjusted the width to produce this screen capture:

I had the same problem with both the bold and regular version of the font. I was able to pull a version of Helvetica Neue Bold from OSX and put it on my device and it displays fine (green background in above picture).

What could be wrong with the either the font file or my code that would cause it to draw this way?

解决方案

I posted a solution that involves patching ttf font file here:

Here's the solution that worked for my custom font which had the same issue in UILabel, UIButton and such. The problem with the font turned out to be the fact that its ascender property was too small compared to the value of system fonts. Ascender is a vertical whitespace above font's characters. To fix your font you will have to download Apple Font Tool Suite command line utilities. Then take your font and do the following:

~$ ftxdumperfuser -t hhea -A d Bold.ttf

This will create Bold.hhea.xml. Open it with a text editor and increase the value of ascender attribute. You will have to experiment a little to find out the exact value that works best for you. In my case I changed it from 750 to 1200. Then run the utility again with the following command line to merge your changes back into the ttf file:

~$ ftxdumperfuser -t hhea -A f Bold.ttf

Then just use the resulting ttf font in your app.

这篇关于自定义安装的字体未在 UILabel 中正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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