Navigation Bar Title Font Size

后端 未结 5 1500
栀梦
栀梦 2020-12-10 03:38

I need to change the size of the Navigation Bar title text for one view controller in my iPhone app. I\'m using iOS5, and tried the following code:

if ([self         


        
相关标签:
5条回答
  • 2020-12-10 04:15

    My example

    guard let sansLightFont = UIFont(name: "OpenSans", size: 20) else { return }
    navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName : sansLightFont]
    
    0 讨论(0)
  • 2020-12-10 04:20

    Since iOS7, I always do like following:

    [[UINavigationBar appearance] setTitleTextAttributes:
        [NSDictionary dictionaryWithObjectsAndKeys:
            [UIFont fontWithName:@"Helvetica-Bold" size:18.0],NSFontAttributeName,
            nil]];
    

    The Keyword UITextAttributeFont has been depressed from iOS7, you'd supposed to replace with NSFontAttributeName.

    You can use [self.navigationController.navigationBar setTitleTextAttributes:]to set someone navigationController's appearence, while [UINavigationBar appearance] setTitleTextAttributes:] works for your whole App directly.(Of course you need to put it in a property position.)

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

    You need to grab the navigation bar property and the use @property(nonatomic, copy) NSDictionary *titleTextAttributes.

    For further info see UINavigationBar Class Reference and Customizing UINavigationBar section.

    To understand better your question: What type of controller do you have?

    EDIT

    [self.navigationController.navigationBar setTitleTextAttributes:...];
    

    if you access the navigationController within a pushed controller.

    [navigationController.navigationBar setTitleTextAttributes:...];
    

    if navigationController is your current UINavigationController. This could be used when for example if you have the following code:

    UINavigationController* navigationController = ...;
    [navigationController.navigationBar setTitleTextAttributes:...];
    
    0 讨论(0)
  • 2020-12-10 04:28

    Swift 2.0:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
            UINavigationBar.appearance().titleTextAttributes = [
                NSFontAttributeName: UIFont(name: "DINNextLTW04-Regular", size: 20)!
            ]
    
            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)!];
    
     }
    
    0 讨论(0)
  • 2020-12-10 04:33

    You got it already,but you can also use following attributes methods.

    For Color,

    NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor RedColor],UITextAttributeTextColor, nil];
    
    self.navigationController.navigationBar.titleTextAttributes = attributes;
    

    For Size,

    NSDictionary *size = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Arial" size:17.0],UITextAttributeFont, nil];
    
    self.navigationController.navigationBar.titleTextAttributes = size;
    

    Thanks.

    For iOS 7 and above

    For Size:

    NSDictionary *size = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Arial" size:17.0],NSFontAttributeName, nil];
    
    self.navigationController.navigationBar.titleTextAttributes = size;
    
    0 讨论(0)
提交回复
热议问题