如何在Swift中更改导航栏字体?

时间:2014-08-19 16:12:22

标签: swift uinavigationbar navigationbar

如何在Swift中更改 NavigationBar 字体?

这是我到目前为止所尝试的,但收到错误(我已经正确地将CaviarDreams实施到项目中):

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

错误说:Use of unresolved identifier 'CaviarDreams

对不起,如果问题非常糟糕。

9 个答案:

答案 0 :(得分:96)

试试这个:

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

编辑:现在,必须解开UIFont才能在这里使用。

答案 1 :(得分:42)

使用Swift,我将其添加到<{p>中的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
    }

希望它有所帮助!

答案 2 :(得分: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)!];

 }

答案 3 :(得分:10)

现在你必须首先打开(!)它,使其不是类型UIFont?

self.navigationController.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "<font-name>", size: <size>)!]

答案 4 :(得分:4)

对于Swift 2.3

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

答案 5 :(得分:3)

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]

}

答案 6 :(得分:1)

Swift 4.2

test1
no space
1var
_temp
ReallyLongNameisInvalidandLongerthan32
IncorrectChars!!asdf
_
underscore_is_fine_

答案 7 :(得分:1)

Swift 4.2

import networkx as nx
import numpy as np
Ag = np.array(
[
    [0, 1, 1, 1, 0, 0, 0, 0],
    [1, 0, 1, 1, 0, 0, 0, 0],
    [1, 1, 0, 1, 0, 1, 0, 0],
    [1, 1, 1, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 1, 0, 0],
    [0, 0, 1, 0, 1, 0, 1, 0],
    [0, 0, 0, 0, 0, 1, 0, 1],
    [0, 0, 0, 0, 0, 0, 1, 0],
]
)
G = nx.from_numpy_array(Ag)
nx.draw(G)

答案 8 :(得分:0)

Swift 4.2

使用它来更改大标题文本:

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

使用它来更改小标题文本(当向下滚动布局时):

self.navigationController!.navigationBar.titleTextAttributes = [.font: UIFont.systemFont(ofSize: 14)]