如何在Swift中更改navigationBar字体?

34

这是我迄今为止尝试过的,但遇到了错误(我已经正确地将CaviarDreams实现到项目中):

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

错误提示: 使用未解决的标识符 'CaviarDreams'

9个回答

98

试试这个:

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

编辑:现在,UIFont必须解包才能在此处使用。

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

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

运行得很好,谢谢。我已经摸索了大约一个小时,试图自己弄清楚它,你知道的,Swift对我来说还有点新。 :P - b3rge
3
注意:NSFontAttributeName现在是String类型,所以这段代码无法正常工作。 - Dehli
@Dehli不知道如何解决这个问题。你有什么建议吗? - Dima Deplov
1
NSFontAttributeName仍然是一个字符串。我认为Dehli的意思是UIFont(...)现在返回一个UIFont?而不是UIFont。有关详细信息,请参阅UIFont.h。benaneesh的答案解决了与这些相关的错误,当您升级到最新的Xcode时会看到。 - jaime
在Swift 4中,NSFontAttributeName被重命名为 titleTextAttributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 23)] - Zhou Haibo

44

使用 Swift,在 AppDelegate.swift 中添加了以下内容:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        UINavigationBar.appearance().titleTextAttributes = [
            NSFontAttributeName: UIFont(name: "DINNextLTW04-Regular", size: 20)!
        ]

        return true
    }

希望它能帮到你!


你的解决方案非常棒。 - anonymox
参照了你的方式,我的朋友 :) - Tim S
更好的方法。谢谢! - haris
2
NSFontAttributeName was now changed to --->> NSAttributedStringKey.font - Cesare

12

Swift 2.0:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        UINavigationBar.appearance().titleTextAttributes = [
            NSFontAttributeName: UIFont(name: "Arial-Regular", size: 30)!
        ]

        return true
    }

或者

 override func viewDidLoad() {
  super.viewDidLoad()

  self.navigationController?.navigationBarHidden =  false
  self.title = "SAMPLE"

//Set Color
  let attributes: AnyObject = [ NSForegroundColorAttributeName: UIColor.redColor()]
  self.navigationController!.navigationBar.titleTextAttributes = attributes as? [String : AnyObject]


//Set Font Size
  self.navigationController!.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Arial", size: 37.0)!];

 }

嗨,Alvin,当我在viewDidLoad()中放入你的字体大小行(xcode 7.3.1),它可以编译通过,但在(模拟器)运行时会崩溃,并显示控制台消息“fatal error: unexpectedly found nil while unwrapping an Optional value”,错误代码为EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP,subcode=0x0)。我在选项卡视图模板的FirstViewController中有导航栏。你有什么建议吗?谢谢。 - rockhammer
现在这个可以工作了:UINavigationBar.appearance().titleTextAttributes = [ NSForegroundColorAttributeName : UIColor.blueColor(), NSFontAttributeName : UIFont(name: "Bradley Hand", size: 28)! ] - rockhammer

10
现在你需要先取消包装(!)它,这样它就不是UIFont?类型了。
self.navigationController.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "<font-name>", size: <size>)!]

4

Swift 4

if let font = UIFont(name: "FontName", size: 16) {

 self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: font]

}

或者,如其他答案所建议的那样,在AppDelegate上执行:

  if let font = UIFont(name: "FontName", size: 16) {

    UINavigationBar.appearance().titleTextAttributes = [
          NSAttributedStringKey.font: font]

}

4

对于Swift 2.3版本

self.navigationController!.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Roboto-Bold", size: 20.0)!, NSForegroundColorAttributeName : UIColor.whiteColor()];

2

Swift 4.2

Change Large Title

self.navigationController!.navigationBar.largeTitleTextAttributes = [.font: UIFont.systemFont(ofSize: 22)]

当页面滚动时,更改布局中的小标题 原始答案: Original Answer
self.navigationController!.navigationBar.titleTextAttributes = [.font: UIFont.systemFont(ofSize: 14)]

1

Swift 4.2

 self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont(name: "Helvetica", size: 18.0)!]

1
你可以只使用.font而不是NSAttributedString.Key.font - Dante Puglisi

1

Swift 4.2

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  // Override point for customization after application launch.

  let font = UIFont(name: "FontName", size: 16) ?? UIFont.systemFont(ofSize: 16)
  UINavigationBar.appearance().titleTextAttributes = [.font: font]
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接