UIButton自定义字体垂直对齐
我有一个 UIButton,它使用自定义字体,该字体在我的视图加载时设置:
- (void)viewDidLoad
{
[super viewDidLoad];
self.searchButton.titleLabel.font = [UIFont fontWithName: @"FONTNAME" size: 15.0 ];
}
我遇到的问题是字体似乎漂浮在中心线上。如果我注释掉这一行,默认字体将显示为垂直居中。但更改为自定义字体会破坏垂直对齐。
我在使用自定义字体的表格单元格上也遇到了同样的问题。
我是否需要在某个地方告诉视图自定义字体不像其他字体那么高?
编辑:我刚刚意识到我使用的字体是 Windows TrueType 字体。我可以在 Mac 上的 TextEdit 中很好地使用它,只是我的应用程序中的对齐有问题
I've got a UIButton
which uses a custom font, which is set when my view loads:
- (void)viewDidLoad
{
[super viewDidLoad];
self.searchButton.titleLabel.font = [UIFont fontWithName: @"FONTNAME" size: 15.0 ];
}
The problem I've got is that the font is appearing to float up of the center line. If I comment out this line, the default font appears vertically centered fine. But changing to the custom font breaks the vertical alignment.
I'm getting the same issue on a Table Cell with a custom font too.
Do I need to tell the view somewhere that the custom font is not as tall as other fonts?
EDIT: I've just realized that the font I'm using is a Windows TrueType Font. I can use it fine in TextEdit on the Mac, only a problem with the alignment in my App
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
自定义安装的字体在 UILabel 中未正确显示。没有给出解决方案。
这是适用于我的自定义字体的解决方案,该字体在 UILabel、UIButton 等中也有同样的问题。事实证明,该字体的问题在于其 ascender 属性与系统字体的值相比太小。上升部分是字体字符上方的垂直空白。要修复字体,您必须下载 Apple Font Tool Suite 命令行实用程序。然后使用您的字体并执行以下操作:
这将创建
Bold.hhea.xml
。使用文本编辑器打开它并增加ascender
属性的值。您必须进行一些试验才能找出最适合您的确切值。在我的例子中,我将其从 750 更改为 1200。然后使用以下命令行再次运行该实用程序,将更改合并回 ttf 文件:然后只需在应用程序中使用生成的 ttf 字体。
OS X El Capitan
Apple 字体工具套件安装程序不再在 OSX El Capitan 上运行,因为 SIP 因为它尝试将二进制文件安装到受保护的目录中。
您必须手动提取
ftxdumperfuser
。首先将 dmg 中的 pkg 复制到本地目录,然后使用 Now 解压OS X Font Tools.pkg
并使用Now 导航到文件夹
fontTools.pkg
并提取有效负载,
现在 < code>ftxdumperfuser 二进制文件位于您的当前文件夹中。您可以将其移动到
/usr/local/bin/
,以便您可以通过以下命令在终端应用程序内的每个文件夹中使用它。A similar problem was discussed at Custom installed font not displayed correctly in UILabel. There was no solution given.
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.
OS X El Capitan
The Apple Font Tool Suite Installer doesn't work anymore on OSX El Capitan because of SIP because it tries to install the binary files into a protected directory.
You have to manually extract
ftxdumperfuser
. First copy the pkg from the dmg to a local directory afterwards unpack theOS X Font Tools.pkg
withNow navigate into the folder
fontTools.pkg
withExtract payload with
Now the
ftxdumperfuser
binary is in your current folder. You could move it to/usr/local/bin/
so that you can use it in every folder inside of the terminal application with the following.我解决了调整顶部内容(不是标题!)插图的问题。
例如:button.contentEdgeInsets = UIEdgeInsetsMake(10.0, 0.0, 0.0, 0.0);
祝你好运!
I solved the problem adjusting the top content (not the title!) inset.
For example: button.contentEdgeInsets = UIEdgeInsetsMake(10.0, 0.0, 0.0, 0.0);
Good luck!
不确定这是否有帮助,因为它可能取决于您的字体,但可能是您的基线未对齐。
Not sure if this will help as it may depend on your font, but it could be that your baseline is misaligned.
我认为这是最好的答案。
禁止使用 ascender、numberOfHMetrics 等...
只需通过 Glyphs 应用程序导入导出
工作完成了。
感谢这个答案:
https://stackoverflow.com/a/16798036/1207684
I think this is the best answer.
no playing with ascender, numberOfHMetrics etc...
just import-export by Glyphs application
and Job done.
Thanks to this answer:
https://stackoverflow.com/a/16798036/1207684
聚会迟到了,但当这个问题第 N 次出现时,我想我应该发布我找到的最简单的解决方案:使用 Python FontTools。
如果您的系统上没有Python 3,请安装它。
安装字体工具
pip3 安装字体工具
FontTools 包含一个 TTX 工具,可实现与 XML 之间的转换。
在同一文件夹中将字体转换为 .ttx
ttx myFontFile.otf
对 .ttx 进行必要的编辑,然后删除 .otf 文件,因为该文件将在下一步中被替换。
将文件转换回 .otf
ttx myFontFile.ttx
Motivation:
The solution by kolyuchi is incomplete, and even with this extended installation flow, running
ftxdumperfuser
resulted in an error on 10.15.2 Catalina.Late to the party, but as this issue hit me for the Nth time, I thought I'd post the simplest solution I've found: using Python FontTools.
Install Python 3 if it's not available on your system.
Install FontTools
pip3 install fonttools
FontTools include a TTX tool which enables conversion to and from XML.
Convert your font to .ttx in the same folder
ttx myFontFile.otf
Make the necessary edits to .ttx and delete the .otf file as this will be replaced in the next step.
Convert the file back to .otf
ttx myFontFile.ttx
Motivation:
The solution by kolyuchi is incomplete, and even with this extended installation flow, running
ftxdumperfuser
resulted in an error on 10.15.2 Catalina.您可以在 Interface Builder 中尝试一下。以下是如何执行此操作的快照 -
正如您所看到的,尝试在 IB 中执行此操作有其自身的好处。
You can try this out in Interface Builder. Here is a snapshot of how to do it -
As you can see trying to do this in IB has its own benefits.
从字体的
otf
切换到ttf
版本可能可以解决此问题,具体取决于字体。Switching from an
otf
to attf
version of the font might be a possible fix for this issue, depending on the font.