简体   繁体   中英

How do I change navigationBar font in Swift?

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)]

Error says: Use of unresolved identifier 'CaviarDreams

Try this:

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

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

Swift 5 (+ safe handling of optional UIFont )

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

Using Swift, I added this to AppDelegate.swift in

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
    }

Hope it helps!

Swift 2.0:

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

        return true
    }

Or

 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)!];

 }

Now you have to unwrap (!) it first so its not of type UIFont? :

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

对于 Swift 2.3

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

Swift 4

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

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

}

Or as the other answer recommended doing it on the AppDelegate:

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

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

}

Swift 4.2

Change Large Title

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

Change Small Title when layout scrolled down

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

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]
}

斯威夫特 4.2

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM