自定义安装的字体在 UILabel 中无法正确显示

发布于 2024-10-26 04:26:59 字数 402 浏览 1 评论 0原文

我正在尝试使用从 Adob​​e 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:

Screen capture of incorrect font rendering

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

醉城メ夜风 2024-11-02 04:27:01

您是否尝试过核心文本?我已经通过 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.

青衫负雪 2024-11-02 04:27:01

我使用 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

嘦怹 2024-11-02 04:27:01

如果您在使用这些命令行实用程序时遇到问题,请尝试在窗口上使用 fontcreator。并从其设置菜单更改字体分配器。

If your are having trouble with these command line utilities then try fontcreator on window. and change font assender from its setting menu.

伤痕我心 2024-11-02 04:27:01

对于因命令而在 Mac OS Mojave 上难以使用 ftxdumperfuser(kolyuchiy 答案)的任何人未找到错误:

  • 从 Apple 下载字体工具包。找到他们在
    https://developer.apple.com/download/more/?q=font< /a>,选择了一个
    XCode 11.
  • 挂载dmg文件
  • 进入磁盘镜像cd /Volumes/macOS\
    Font\ Tools
  • 将包解压到您选择的文件夹中:pkgutil
    --expand-full macOS\ Font\ Tools.pkg ~/font-tools
  • CLI 工具现在可在 ~/font-tools/FontCommandLineTools.pkg/Payload 中使用,您可以
    可以将该文件夹添加到您的路径(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:

  • Download the font tools package from Apple. Found them at
    https://developer.apple.com/download/more/?q=font, picked the one for
    XCode 11.
  • Mount the dmg file
  • Enter the disk image cd /Volumes/macOS\
    Font\ Tools
  • Extract the package to a folder of your choosing: pkgutil
    --expand-full macOS\ Font\ Tools.pkg ~/font-tools
  • The CLI tools are now available in ~/font-tools/FontCommandLineTools.pkg/Payload, you
    may add the folder to your path (export PATH="$PATH:$HOME/font-tools/FontCommandLineTools.pkg/Payload"), or copy the utils to your bin
    folder.
梦一生花开无言 2024-11-02 04:27:01

我的 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.

铁憨憨 2024-11-02 04:27:00

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

这是适用于具有相同问题的自定义字体的解决方案在 UILabel、UIButton 等中。事实证明,该字体的问题在于其 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 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.

吃颗糖壮壮胆 2024-11-02 04:27:00

所以这是 kolyuchiy 答案的修改版本。

我使用 Glyphs 打开字体,然后将其导出而不进行任何修改。不知何故,垂直对齐问题神奇地消失了!

更好的是,新字体可以很好地与 sizeWithFont: 等方法配合使用,因此它不会出现 Joshua 提到的问题。

我使用提到的命令 kolyuchiy 查看了 HHEA 表,发现 Glyphs 不仅修改了 ascender,还修改了 lineGapnumberOfHMetrics为我。

这是原始数据,之前:

versionMajor="1"
versionMinor="0"
ascender="780"
descender="-220"
lineGap="200"
advanceWidthMax="1371"
minLeftSideBearing="-73"
minRightSideBearing="-52"
xMaxExtent="1343"
caretSlopeRise="1"
caretSlopeRun="0"
caretOffset="0"
metricDataFormat="0"
numberOfHMetrics="751"

和之后:

versionMajor="1"
versionMinor="0"
ascender="980"
descender="-220"
lineGap="0"
advanceWidthMax="1371"
minLeftSideBearing="-73"
minRightSideBearing="-52"
xMaxExtent="1343"
caretSlopeRise="1"
caretSlopeRun="0"
caretOffset="0"
metricDataFormat="0"
numberOfHMetrics="748"

所以这个故事的寓意是——不要只增加上升器,还要修改其他相关值。

我不是排版专家,所以我无法真正解释原因和方式。如果有人能提供更好的解释,将不胜感激! :)

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 also lineGap and numberOfHMetrics for me.

Here's the raw data, before:

versionMajor="1"
versionMinor="0"
ascender="780"
descender="-220"
lineGap="200"
advanceWidthMax="1371"
minLeftSideBearing="-73"
minRightSideBearing="-52"
xMaxExtent="1343"
caretSlopeRise="1"
caretSlopeRun="0"
caretOffset="0"
metricDataFormat="0"
numberOfHMetrics="751"

and after:

versionMajor="1"
versionMinor="0"
ascender="980"
descender="-220"
lineGap="0"
advanceWidthMax="1371"
minLeftSideBearing="-73"
minRightSideBearing="-52"
xMaxExtent="1343"
caretSlopeRise="1"
caretSlopeRun="0"
caretOffset="0"
metricDataFormat="0"
numberOfHMetrics="748"

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! :)

醉南桥 2024-11-02 04:27:00

iOS 6 尊重字体的 lineGap 属性,而 iOS 7 则忽略它。因此,只有行距为 0 的自定义字体才能在两个操作系统上正常工作。

解决办法是把lineGap设置为0,同时将ascender设置得相应变大。根据上面的答案,一种解决方案是从 Glyphs 导入和导出。但是,请注意,该应用程序的未来版本可能会修复此“错误”。

更强大的解决方案是根据此 帖子。具体来说,

  1. 安装 OS X 字体工具。
  2. 将字体规格转储到文件中:ftxdumperfuser -t hhea -A d YOUR_FONT.ttf
  3. 在编辑器中打开转储的文件。
  4. 通过添加 lineGap 属性的值来编辑 ascender 属性。例如,如果 lineGap 为 200,ascender 为 750,则将 ascender 设置为 950。
  5. 设置 lineGap为 0。
  6. 将更改合并到字体中: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,

  1. Install OS X Font Tools.
  2. Dump the font metrics to a file: ftxdumperfuser -t hhea -A d YOUR_FONT.ttf
  3. Open the dumped file in an editor.
  4. Edit the ascender property by adding the value of the lineGap property to it. For example, if the lineGap is 200 and the ascender is 750, make the ascender 950.
  5. Set the lineGap to 0.
  6. Merge the changes into the font: ftxdumperfuser -t hhea -A f YOUR_FONT.ttf

Once you do this, you might have to adjust your UI accordingly.

丑疤怪 2024-11-02 04:27:00

对于那些运行 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

旧情别恋 2024-11-02 04:27:00
  1. 在此处下载并安装 Apple 的字体工具:https://developer.apple.com/downloads /index.action?q=font (下载链接在底部)
  2. 打开终端并 cd 到您的字体所在的位置
  3. 运行此命令:ftxdumperfuser -t hhea -A d MY_FONT_NAME.ttf
  4. 现在您有包含某些字体属性的 xml 文件,在文本编辑器中对其进行编辑
  5. 搜索“lineGap”属性并将其值添加 200
  6. 保存 xml 文件
  7. 运行以下命令:ftxdumperfuser -t hhea -A f MY_FONT_NAME.ttf
  8. 删除 xml file
  9. 在 iOS 6 上尝试配置的字体,看看是否看起来更好。
  10. 如果需要,您可以返回步骤 3 并添加/减去“lineGap”属性。 (我最终在我的配置中添加了 250)
  1. Download and Install Apple's Font Tools here: https://developer.apple.com/downloads/index.action?q=font (the download link is in the bottom)
  2. Open the terminal and cd your way to where your font is
  3. Run this command: ftxdumperfuser -t hhea -A d MY_FONT_NAME.ttf
  4. Now you have an xml file with some of the font's properties, edit it in your text editor
  5. Search for the "lineGap" property and add 200 to its value
  6. Save the xml file
  7. Run this command: ftxdumperfuser -t hhea -A f MY_FONT_NAME.ttf
  8. Delete the xml file
  9. Try the configured font on iOS 6 and see if it looks better.
  10. If you need, you can go back to step 3 and add/subtract to the "lineGap" property. (I ended up adding 250 to my configuration)
勿挽旧人 2024-11-02 04:27:00

我们的一种自定义字体也遇到了同样的问题。我们还通过编辑字体上升属性“修复”了该问题。然而,我们发现这会产生其他问题和布局问题。例如,当使用我们的上升编辑字体时,根据标签高度动态设置单元格高度会爆炸。

我们最终所做的是更改 UIButton contentEdgetInsets 属性。

yourButton.contentEdgeInsets = UIEdgeInsetsMake(-10, 0, 0, 0);

不确定哪种方法更好,但只是想分享另一种方法来解决问题。

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.

yourButton.contentEdgeInsets = UIEdgeInsetsMake(-10, 0, 0, 0);

Not sure which method is better, but just wanted to share another way to fix the problem.

屋顶上的小猫咪 2024-11-02 04:27:00

感谢这个答案,我解决了 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):

enter image description here

Just delete all of this alignment zones (or some of them) and it will fix this problem.
Worked perfectly for me.

演多会厌 2024-11-02 04:27:00

从标签文本创建属性文本对我来说是解决方案。这是一个扩展:

extension UILabel {
    /// You can call with or without param values; without will use default 2.0
    func setLineSpacing(lineSpacing: CGFloat = 2.0, lineHeightMultiple: CGFloat = 2.0) {
        guard let labelText = self.text else { return }
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = lineSpacing
        paragraphStyle.lineHeightMultiple = lineHeightMultiple
        let attributedString:NSMutableAttributedString
        if let labelattributedText = self.attributedText {
            attributedString = NSMutableAttributedString(attributedString: labelattributedText)
        } else {
            attributedString = NSMutableAttributedString(string: labelText)
        }
        // (Swift 4.2 and above) Line spacing attribute
        attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedString.length))
        self.attributedText = attributedString
    }
}

对于我的自定义字体,我得到了我需要的结果:

self.myLabel.setLineSpacing(lineSpacing: 1.2, lineHeightMultiple: 1.2)

这通过使用本机提供的 NSMutableParagraphStyle() 来工作,其中包含行高和间距属性(可以通过 @IBOutlet 访问)如果您没有对标签进行编程,也可以在情节提要中使用 属性)。

Creating attributed text from your labels text was the fix for me. Heres an extension:

extension UILabel {
    /// You can call with or without param values; without will use default 2.0
    func setLineSpacing(lineSpacing: CGFloat = 2.0, lineHeightMultiple: CGFloat = 2.0) {
        guard let labelText = self.text else { return }
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = lineSpacing
        paragraphStyle.lineHeightMultiple = lineHeightMultiple
        let attributedString:NSMutableAttributedString
        if let labelattributedText = self.attributedText {
            attributedString = NSMutableAttributedString(attributedString: labelattributedText)
        } else {
            attributedString = NSMutableAttributedString(string: labelText)
        }
        // (Swift 4.2 and above) Line spacing attribute
        attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedString.length))
        self.attributedText = attributedString
    }
}

For my custom font I got the result I need from:

self.myLabel.setLineSpacing(lineSpacing: 1.2, lineHeightMultiple: 1.2)

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文