Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation Bar Title Font Size

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.

like image 846
jcrowson Avatar asked Mar 08 '12 16:03

jcrowson


2 Answers

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

 }
like image 77
Alvin George Avatar answered Oct 01 '22 10:10

Alvin George


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:...];
like image 33
Lorenzo B Avatar answered Oct 01 '22 10:10

Lorenzo B