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.
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)!];
}
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:...];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With