如何在Swift中更改navigationBar字体? [英] How do i change navigationBar font in Swift?

查看:223
本文介绍了如何在Swift中更改navigationBar字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Swift中更改 NavigationBar 字体?

How do I change the NavigationBar font in Swift?

这是我到目前为止尝试过的,但是收到一个错误(我已经正确地对项目实现了CaviarDreams):

This is what I have tried so far, but receiving an error (I have correctly implemented CaviarDreams to the project):

self.navigationController.navigationBar.titleTextAttributes = NSFontAttributeName[UIFont .fontWithName(CaviarDreams.ttf, size: 20)]

错误说:Use of unresolved identifier 'CaviarDreams

对不起,如果问题很严重.

Sorry if the question is extremely bad.

推荐答案

尝试一下:

self.navigationController.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "CaviarDreams", size: 20)!]

现在,必须解开UIFont才能在此处使用.

Now, UIFont must be unwrapped to be able to be used here.

Swift 5 (+安全处理可选的UIFont)

Swift 5 (+ safe handling of optional UIFont)

self.navigationController?.navigationBar.titleTextAttributes = [ NSAttributedString.Key.font: UIFont(name: "Caviar-Dreams", size: 20) ?? UIFont.systemFont(ofSize: 20)]

这篇关于如何在Swift中更改navigationBar字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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