自定义安装的字体在 UILabel 中无法正确显示
我正在尝试使用从 Adobe Font Collection Pro Package 获得的 Helvetica Neue Condensed 字体。不幸的是,当我在 UILabel
中使用它时,它似乎绘制不正确。
行高似乎计算正确(我认为),但是当显示字体时,它与边界框的最顶部对齐。我调用了 [myLabel sizeToFit]
并仅调整了宽度以生成此屏幕截图:
我对字体的粗体和常规版本都遇到了同样的问题。我能够从 OSX 中提取 Helvetica Neue Bold 版本并将其放在我的设备上,并且它显示良好(上图中的绿色背景)。
字体文件或我的代码可能有什么问题导致它以这种方式绘制?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
您是否尝试过核心文本?我已经通过 Core Text 渲染自定义字体取得了一些成功,但我不知道它是否适合您的情况。
Have you tried Core Text? I've had some success rendering custom fonts through Core Text, but I don't know if it would fit your situation.
我使用 https://github.com/fonttools/fonttools - 非常易于使用且免费。就我而言,“hhea”表中“ascender”=1000 和“lineGap”=0 的更改就达到了目的。
基于 Trevor Harmon 的文章 https://medium.com/@thetrevorharmon/how-to-use-apples-font-tools-to-tweak-a-font-a386600255ae
I used https://github.com/fonttools/fonttools - very easy to use and free. In my case, the change of 'ascender'=1000 and 'lineGap'=0 in 'hhea' table did the trick.
Based on article from Trevor Harmon https://medium.com/@thetrevorharmon/how-to-use-apples-font-tools-to-tweak-a-font-a386600255ae
如果您在使用这些命令行实用程序时遇到问题,请尝试在窗口上使用 fontcreator。并从其设置菜单更改字体分配器。
If your are having trouble with these command line utilities then try fontcreator on window. and change font assender from its setting menu.
对于因命令而在 Mac OS Mojave 上难以使用 ftxdumperfuser(kolyuchiy 答案)的任何人未找到错误:
https://developer.apple.com/download/more/?q=font< /a>,选择了一个
XCode 11.
Font\ Tools
--expand-full macOS\ Font\ Tools.pkg ~/font-tools
可以将该文件夹添加到您的路径(
export PATH="$PATH:$HOME/font-tools/FontCommandLineTools.pkg/Payload"
),或将 utils 复制到您的 bin文件夹。
For anyone who are struggling to use
ftxdumperfuser
(kolyuchiy answer) on Mac OS Mojave because of command not found error:https://developer.apple.com/download/more/?q=font, picked the one for
XCode 11.
Font\ Tools
--expand-full macOS\ Font\ Tools.pkg ~/font-tools
may add the folder to your path (
export PATH="$PATH:$HOME/font-tools/FontCommandLineTools.pkg/Payload"
), or copy the utils to your binfolder.
我的 Sprite Kit 游戏中的标志性“FontAwesome”字体也遇到了类似的问题。
将 SKLabelNode 的 SKLabelVerticalAlignmentMode 属性设置为 .Center 对我有用。
myLabel.verticalAlignmentMode = SKLabelVerticalAlignmentMode.Center
只是想分享以防有人遇到同样的问题。
I had a similar issue with iconic "FontAwesome" font in my Sprite Kit game.
Setting the SKLabelNode's SKLabelVerticalAlignmentMode property to .Center worked for me.
myLabel.verticalAlignmentMode = SKLabelVerticalAlignmentMode.Center
Just wanted to share in case somebody would be struggling with the same problem.
我发布了一个涉及修补 ttf 字体文件的解决方案此处:
这是适用于具有相同问题的自定义字体的解决方案在 UILabel、UIButton 等中。事实证明,该字体的问题在于其 ascender 属性与系统字体的值相比太小。上升部分是字体字符上方的垂直空白。要修复字体,您必须下载 Apple Font Tool Suite 命令行实用程序。然后使用您的字体并执行以下操作:
这将创建
Bold.hhea.xml
。使用文本编辑器打开它并增加ascender
属性的值。您必须进行一些试验才能找出最适合您的确切值。在我的例子中,我将其从 750 更改为 1200。然后使用以下命令行再次运行该实用程序,将更改合并回 ttf 文件:然后只需在应用程序中使用生成的 ttf 字体。
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:
This will create
Bold.hhea.xml
. Open it with a text editor and increase the value ofascender
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:Then just use the resulting ttf font in your app.
所以这是 kolyuchiy 答案的修改版本。
我使用 Glyphs 打开字体,然后将其导出而不进行任何修改。不知何故,垂直对齐问题神奇地消失了!
更好的是,新字体可以很好地与
sizeWithFont:
等方法配合使用,因此它不会出现 Joshua 提到的问题。我使用提到的命令 kolyuchiy 查看了 HHEA 表,发现 Glyphs 不仅修改了
ascender
,还修改了lineGap
和numberOfHMetrics
为我。这是原始数据,之前:
和之后:
所以这个故事的寓意是——不要只增加上升器,还要修改其他相关值。
我不是排版专家,所以我无法真正解释原因和方式。如果有人能提供更好的解释,将不胜感激! :)
So this is a modified version of kolyuchiy's answer.
I opened my font with Glyphs, and then exported it without modifying anything. Somehow, magically, the vertical alignment issue was gone!
What's better is that the new font plays nicely with methods like
sizeWithFont:
, so it doesn't have the issues mentioned by Joshua.I took a look at the HHEA table with the command kolyuchiy mentioned, and noticed that Glyphs modified not just the
ascender
, but alsolineGap
andnumberOfHMetrics
for me.Here's the raw data, before:
and after:
So the moral of the story- don't just increase the ascender, but modify other related values as well.
I'm no typography expert so I can't really explain the why and how. If anyone can provide a better explanation it'd be greatly appreciated! :)
iOS 6 尊重字体的 lineGap 属性,而 iOS 7 则忽略它。因此,只有行距为 0 的自定义字体才能在两个操作系统上正常工作。
解决办法是把lineGap设置为0,同时将ascender设置得相应变大。根据上面的答案,一种解决方案是从 Glyphs 导入和导出。但是,请注意,该应用程序的未来版本可能会修复此“错误”。
更强大的解决方案是根据此 帖子。具体来说,
ftxdumperfuser -t hhea -A d YOUR_FONT.ttf
lineGap
属性的值来编辑ascender
属性。例如,如果lineGap
为 200,ascender
为 750,则将ascender
设置为 950。lineGap
为 0。ftxdumperfuser -t hhea -A f YOUR_FONT.ttf
执行此操作后,您可能需要调整 UI 因此。
iOS 6 honors the font's lineGap property, while iOS 7 ignores it. So only custom fonts with a line gap of 0 will work correctly across both operating systems.
The solution is to make the lineGap 0 and make the ascender correspondingly larger. Per the answer above, one solution is to import and export from Glyphs. However, note that a future version of the app might fix this "bug".
A more robust solution is to edit the font yourself, per this post. Specifically,
ftxdumperfuser -t hhea -A d YOUR_FONT.ttf
ascender
property by adding the value of thelineGap
property to it. For example, if thelineGap
is 200 and theascender
is 750, make theascender
950.lineGap
to 0.ftxdumperfuser -t hhea -A f YOUR_FONT.ttf
Once you do this, you might have to adjust your UI accordingly.
对于那些运行 OS X El Capitan 并来到此线程的人,您可能已经注意到 Apple 字体工具套件不再兼容(至少目前如此)。
但您仍然可以使用免费字体编辑软件FontForge。
使用 FontForge 打开字体并在顶部菜单中选择“元素”,然后转到“字体信息”>“字体信息”>“字体信息”。操作系统/2>指标。您要在此处编辑 HHEad Line Gap 和 HHEad Ascent Offset 值。
完成必要的编辑后,您只需在“文件”>“导出”中导出字体即可。生成字体并选择正确的字体格式
For those running OS X El Capitan and coming to this thread, you might have noticed that the Apple Font Tool Suite is no longer compatible (at least for now).
But you can still perform the changes described by kolyuchiy and Joseph Lin with free font editing software FontForge.
Open the font with FontForge and select Element in the top menu, then go to Font Info > OS/2 > Metrics. There you want to edit the HHEad Line Gap and HHead Ascent Offset values.
Once you've done the necessary edits you can just export the font in File > Generate Fonts and select the right font format
我们的一种自定义字体也遇到了同样的问题。我们还通过编辑字体上升属性“修复”了该问题。然而,我们发现这会产生其他问题和布局问题。例如,当使用我们的上升编辑字体时,根据标签高度动态设置单元格高度会爆炸。
我们最终所做的是更改 UIButton contentEdgetInsets 属性。
不确定哪种方法更好,但只是想分享另一种方法来解决问题。
We had the same issue with one of our custom fonts. We also "fixed" the problem by editing the font ascender property. However, we found that this created other problems and layout issues. For example dynamically setting cell height based on label height would blow up when using our ascender edited font.
What we ended up doing was changing the UIButton contentEdgetInsets property.
Not sure which method is better, but just wanted to share another way to fix the problem.
感谢这个答案,我解决了 Glyphs 的问题,但有点不同。
我用 Glyphs 打开我的字体(也适用于 Glyphs mini),并在那里找到了这个部分(这来自 Glyphs mini,要到达那里,请按右上角的 i 按钮):
只需删除所有这些对齐区域(或其中一些)即可解决此问题。
非常适合我。
Thanks to the this answer I fixed my problem with Glyphs, but a little bit differently.
I opened my font with Glyphs (also works with Glyphs mini) and found this section there (this from Glyphs mini, to get there push i button in the right top corner):
Just delete all of this alignment zones (or some of them) and it will fix this problem.
Worked perfectly for me.
从标签文本创建属性文本对我来说是解决方案。这是一个扩展:
对于我的自定义字体,我得到了我需要的结果:
这通过使用本机提供的
NSMutableParagraphStyle()
来工作,其中包含行高和间距属性(可以通过@IBOutlet 访问)如果您没有对标签进行编程,也可以在情节提要中使用
属性)。Creating attributed text from your labels text was the fix for me. Heres an extension:
For my custom font I got the result I need from:
This works by using the native provided
NSMutableParagraphStyle()
which contains line height and spacing properties (which are accessible as@IBOutlet
properties in the Storyboard too if you are not programming your labels).