自定义字体错误 [英] Custom Fonts Bug

查看:96
本文介绍了自定义字体错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR:在情节提要"/xib中使用自定义字体之前,无法以编程方式使用自定义字体.

注意:我已签出,已尝试答案,他们没有用.我还确保他们是目标成员.

Note: I've checked out this, tried the answers and they didn't work. I've also made sure that they're in target membership.

在将段控件标题更改为自定义字体时,我注意到了一个奇怪的错误:

I've noticed a strange bug while changing segment control title a custom font:

segmentedControl.titleTextAttributes = NSDictionary(objects: [UIFont.init(name: "Comfortaa-Regular",
                                                                          size: UIFont.systemFontSize)!,
                                                              UIColor.white],
                                                    forKeys: [NSAttributedStringKey.font as NSCopying,
                                                              NSAttributedStringKey.foregroundColor as NSCopying]) as? [AnyHashable : Any]

找不到字体,因此展开失败.但是字体可以在情节提要中看到.

It couldn't find the font, so unwrapping has failed. But the font could be seen in the Storyboard.

已正确添加到项目中,这里是CopyBundle和InfoList:

It's properly added to the project, here's the CopyBundle and InfoList:

这就是陷阱;如果我在情节提要中使用它,它将以字体系列显示:

So here's the catch; if I use it in the Storyboard, it is shown in the font families:

但是,如果没有显示,则不会显示-在这里我改为浅色,并且黑体消失了-,因此无法以编程方式使用.

But if not, it's not shown -here I've changed to light and bold has disappeared-, and cannot be used programmatically.

我正在使用Xcode 9.0(9A235)和Swift4.我还检查了OpenRadar,找不到与此相关的提交.

I'm using Xcode Version 9.0 (9A235), and Swift 4. I've also checked OpenRadar and couldn't find a submission regarding this.

提到的字体: Comfortaa

推荐答案

@EDUsta,我只是尝试使用给定的字体及其工作正常,没有问题,给出了我遵循的步骤:

@EDUsta, I just tried with given font and its work ok, no issue in it, giving the step which i followed:

  1. Comfortaa-Bold.ttfComfortaa-Light.ttfComfortaa-Regular.ttf字体文件添加到项目中.
  1. Add the Comfortaa-Bold.ttf, Comfortaa-Light.ttf, Comfortaa-Regular.ttf Font files to project.

2.在info.plist文件中添加字体的条目

2.Add the entries of Fonts in info.plist file

确保在项目目标中添加了字体.

Make sure the font added in project target.

之后,使用下面给出的Swift代码在UILabelUISegmentedControl上应用字体:

After it, apply the fonts on UILabel and UISegmentedControl using Swift code given below:

   let fontFirst = UIFont(name: "Comfortaa-Bold", size: 16.0)!
    segmentFont.setTitleTextAttributes([NSAttributedStringKey.font : fontFirst, NSAttributedStringKey.foregroundColor : UIColor.red],
                                       for: .normal)

    labelBold.font = UIFont(name: "Comfortaa-Bold", size: 16.0)
    labelLight.font = UIFont(name: "Comfortaa-Light", size: 16.0)
    labelRegular.font = UIFont(name: "Comfortaa-Regular", size: 16.0)

它工作正常,您可以在下面的屏幕截图中检入:

its work perfectly, you can check in below screenshot image:

供您参考,我正在添加完整的项目此处:

For your reference i am adding the complete project here:

这篇关于自定义字体错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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