iOS change navigation bar title font and color

后端 未结 18 668
青春惊慌失措
青春惊慌失措 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 08:56

    Here is an answer for your question:

    Move your code to below method because navigation bar title updated after view loaded. I tried adding above code in viewDidLoad doesn't work, it works fine in viewDidAppear method.

      -(void)viewDidAppear:(BOOL)animated{}
    
    0 讨论(0)
  • 2020-12-04 08:56

    For Objective-C to set Font and Color

    - (void)_setup {
        NSDictionary *barButtonTitleAttributes = @{
                                                   NSForegroundColorAttributeName : [UIColor whiteColor],
                                                   NSFontAttributeName :[UIFont fontWithName:@"Lato-Regular" size:15.0]
                                                   };
        [self.navigationBar setTitleTextAttributes:barButtonTitleAttributes];
    
    }
    
    0 讨论(0)
  • 2020-12-04 08:58

    The correct way to change the title font (and color) is:

    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSForegroundColorAttributeName:[UIColor redColor],
    NSFontAttributeName:[UIFont fontWithName:@"mplus-1c-regular" size:21]}];
    

    Edit: Swift 4.2

    self.navigationController?.navigationBar.titleTextAttributes =
    [NSAttributedString.Key.foregroundColor: UIColor.red,
     NSAttributedString.Key.font: UIFont(name: "mplus-1c-regular", size: 21)!]
    

    Edit: Swift 4

    self.navigationController?.navigationBar.titleTextAttributes =
    [NSAttributedStringKey.foregroundColor: UIColor.red,
     NSAttributedStringKey.font: UIFont(name: "mplus-1c-regular", size: 21)!]
    

    Swift 3:

    self.navigationController?.navigationBar.titleTextAttributes = 
    [NSForegroundColorAttributeName: UIColor.redColor(),
     NSFontAttributeName: UIFont(name: "mplus-1c-regular", size: 21)!]
    
    0 讨论(0)
  • 2020-12-04 08:58

    Add this extension

    extension UINavigationBar {
        func changeFont() {
            self.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont(name:"Poppins-Medium", size: 17)!]
        }
    }
    

    Add the following line in viewDidLoad()

    self.navigationController?.navigationBar.changeFont()
    
    0 讨论(0)
  • 2020-12-04 09:02

    My Swift code for change Navigation Bar title:

    let attributes = [NSFontAttributeName : UIFont(name: "Roboto-Medium", size: 16)!, NSForegroundColorAttributeName : UIColor.whiteColor()]
    self.navigationController.navigationBar.titleTextAttributes = attributes
    

    And if you want to change background font too then I have this in my AppDelegate:

    let attributes = [NSFontAttributeName : UIFont(name: "Roboto-Medium", size: 16)!, NSForegroundColorAttributeName : UIColor.whiteColor()]
    UIBarButtonItem.appearance().setTitleTextAttributes(attributes, forState: UIControlState.Normal)
    
    0 讨论(0)
  • 2020-12-04 09:08

    Try this:

    NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [UIColor whiteColor],NSForegroundColorAttributeName,
                                    [UIColor whiteColor],NSBackgroundColorAttributeName,nil];
    self.navigationController.navigationBar.titleTextAttributes = textAttributes;
    
    0 讨论(0)
提交回复
热议问题