iOS change navigation bar title font and color

后端 未结 18 667
青春惊慌失措
青春惊慌失措 2020-12-04 08:40

So i have this code that should change the nav bar title font, but it doenst

    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFon         


        
相关标签:
18条回答
  • 2020-12-04 09:08

    Anyone needs a Swift 3 version. redColor() has changed to just red.

    self.navigationController?.navigationBar.titleTextAttributes =
            [NSForegroundColorAttributeName: UIColor.red,
             NSFontAttributeName: UIFont(name: "{your-font-name}", size: 21)!]
    
    0 讨论(0)
  • 2020-12-04 09:10

    I had one problem because when I tried to change my NavigationItem title programmatically i was not able to find my family font (tried many things but impossible to make it run correctly) so I found one workaround very nice and easy in storyboard.

    1. Firstly you add under Navigation Item one view in middle and don't forget to set backGroundColor to clear color to have the same color of navBar:

    1. Then you add one Label which you can edit (set color of text, font, size...) in this view
    2. You add constraints to label (Top = 0, Bottom = 0, Trailing = 0 and Leading = 0) to View and center text of label

    Finally you should have something like that in document outline:

    And something like that in your ViewController:

    Hope it can help.

    0 讨论(0)
  • 2020-12-04 09:10

    Don't forget to add the Raw values of the keys to avoid compile errors.

        let textAttributes:[NSAttributedStringKey: Any] = [NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue):UIColor.blue, NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue):UIFont(name:"OpenSans", size: 17)!]
        navigationController?.navigationBar.titleTextAttributes = textAttributes
    
    0 讨论(0)
  • 2020-12-04 09:12

    Swift 4.2

    self.navigationController?.navigationBar.titleTextAttributes =
            [NSAttributedString.Key.foregroundColor: UIColor.white,
             NSAttributedString.Key.font: UIFont(name: "LemonMilklight", size: 21)!]
    
    0 讨论(0)
  • 2020-12-04 09:13

    ADD this single line code in your App Delegate - Did Finish Lauch. It will change Font, color of navigation bar throughout the application.

    UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font: UIFont(name: "YOUR FONT NAME", size: 25.0)!]
    
    0 讨论(0)
  • 2020-12-04 09:13

    This one for alternative to Swift 4 (already answer by @Josh):

    let titleTextAttributed: [NSAttributedStringKey: Any] = [.foregroundColor: UIColor.red, .font: UIFont(name: "AvenirNext-Regular", size: 20) as Any]
    navigationController?.navigationBar.titleTextAttributes = titleTextAttributed
    
    0 讨论(0)
提交回复
热议问题