尝试以下步骤: 代码在Swift 3
中测试。
步骤1:将自定义字体添加到项目中(确保选择了添加到目标)。 我使用“PermanentMarker.ttf”,“Pecita.otf”和“AROLY.ttf”字体作为测试字体。
注意:支持的字体类型为ttf和otf(这两种字体类型都应该可以工作)
步骤2:修改application-info.plist
文件。在新行中添加键“Fonts provided by application”,并将“PermanentMarker.ttf”作为数组“Fonts provided by application”中的新项目添加。
您的plist
应如下所示
![enter image description here](https://istack.dev59.com/i4hFK.webp)
现在这个字体将在Interface Builder中可用。 要在代码中使用自定义字体,我们需要按名称引用它,但是名称通常与字体的文件名不同。
现在,您可以从viewController
访问自定义字体。 我通过像下面这样在Storyboard中放置一个UIlabel来测试字体。
更新2:工作解决方案
在导入您的自定义字体并更新您的plist
之后。 从storyBoard
中选择label
,转到Attributes Inspector
,在Label
下选择Text type
>更改为Attributed
,然后从列表中选择您的custom font
。
![enter image description here](https://istack.dev59.com/qgzMA.webp)
![enter image description here](https://istack.dev59.com/XRhyN.gif)
![enter image description here](https://istack.dev59.com/wNVim.webp)
输出:
![enter image description here](https://istack.dev59.com/xTSoD.gif)
更新1
如果您的自定义字体仍未在Xcode字体列表
中列出。 请查看与您问题相关的链接
注意:即使您的interface Builder
中未显示,仍然可以将BebasNeue
或custom font
按程序指定分配给您的标签或按钮等。 如果设置对象字体时遇到问题,请尝试以下方法。
将字体分配给UILabel:
label?.font = UIFont(name: "BebasNeue", size: 35) // Set to any size
给UIButton分配字体:
button.titleLabel?.font = UIFont(name: "BebasNeue", size: 35)
给 UITextField 分配字体:
textField.font = UIFont(name: "BebasNeue", size: 25)
为UINavigationBar分配字体:
navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "BebasNeue", size: 25)!, NSForegroundColorAttributeName: UIColor.red]
为UIToolBar分配字体:
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "BebasNeue", size: 25.0)!], for: UIControlState.normal)
输出:
![在这里输入图片描述](https://istack.dev59.com/ip2wX.gif)