13
votes

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.tabBarItem respondsToSelector:@selector(setTitleTextAttributes:)]) {
    NSLog(@"*** Support method(iOS 5): setTitleTextAttributes:");
    [self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                [UIFont fontWithName:@"AmericanTypewriter" size:20.0f], UITextAttributeFont,
                                                [UIColor blackColor], UITextAttributeTextColor,
                                                [UIColor grayColor], UITextAttributeTextShadowColor,
                                                [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)], UITextAttributeTextShadowOffset,
                                                nil]];
}

However this only applies to the tabBarItem.

5

5 Answers

25
votes

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;
14
votes

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:...];
10
votes

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

4
votes

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
votes

My example

guard let sansLightFont = UIFont(name: "OpenSans", size: 20) else { return }
navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName : sansLightFont]