Change the navigation bar's font - iphone

The question is plain easy and simple, the answer unfortunately not.
How can you change the font of the text in the UINavigationBar?

From iOS 7 and later:
NSShadow* shadow = [NSShadow new];
shadow.shadowOffset = CGSizeMake(0.0f, 1.0f);
shadow.shadowColor = [UIColor redColor];
[[UINavigationBar appearance] setTitleTextAttributes: #{
NSForegroundColorAttributeName: [UIColor greenColor],
NSFontAttributeName: [UIFont fontWithName:#"Helvetica" size:20.0f],
NSShadowAttributeName: shadow
}];
From iOS 5 and later:
[[UINavigationBar appearance] setTitleTextAttributes: #{
UITextAttributeTextColor: [UIColor greenColor],
UITextAttributeTextShadowColor: [UIColor redColor],
UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],
UITextAttributeFont: [UIFont fontWithName:#"Helvetica" size:20.0f]
}];
Earlier than iOS 5:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 400, 44)];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor whiteColor];
label.text=self.title;
self.navigationItem.titleView = label;
[label release];

If you wanted to change the font in the Interface Builder itself (without any code) here is the way to do it in Xcode6:
1.) Find the Navigation Bar view under the Navigation Controller Scene
2.) Change the Title Font, Color and Shadow attributes in the Attributes Inspector.

The above answer works. I would add the following line before the last line. If I don't, it seems the label is center-aligned incorrectly if there is a back button on the left side but no right button.
...
[self.navigationItem.titleView sizeToFit];
[label release]; // not needed if you are using ARC

Updated for iOS 7:
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,
shadow, NSShadowAttributeName,
[UIFont fontWithName:#"HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil]];
courtesy of:
http://www.appcoda.com/customize-navigation-status-bar-ios-7/

Not sure why all the answers included the shadow. Adding the lines that manipulate the shadow does nothing in respect to changing text font. These 2 lines of code will work for iOS 8.4 and Swift
let attributesDictionary = [NSFontAttributeName: UIFont(name: "Helvetica Neue", size: 14)!]
navigationController!.navigationBar.titleTextAttributes = attributesDictionary
The titleTextAttributes stores a dictionary that will dictate the font, color, size, and other attributes of the navigation bar's title.

As of iOS 5 you can use the appearance proxy.
The answer is in a duplicate of this question: https://stackoverflow.com/a/12364740/883413

NSShadow *shadow = [NSShadow new];
[shadow setShadowColor: [UIColor clearColor]];
[shadow setShadowOffset: CGSizeMake(0.0f, 1.0f)];
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"TimeBurner" size:27.0f], NSFontAttributeName,
[UIColor whiteColor], NSForegroundColorAttributeName,
shadow, NSShadowAttributeName,nil]];

Related

How to change the font size of the tab bar controller item name?

I am doing an app based on tabbarController. I have a 3 tabbar items.
My question is: How can I change the font-style for the title on the tab-bar item?
[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]];
This will change ur UITabBarItem fonts once and for all throughout the app
For Swift use this in AppDelegate's didFinishLaunching:
Swift 3:
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.blue,NSFontAttributeName: UIFont(name: "Montserrat", size: 11)!], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.red,NSFontAttributeName: UIFont(name: "Montserrat", size: 11)!], for: .selected)
Sorry, I dont think there's a way to do this. If you're desperate, you'll need to write your own tab bar.
Sadly, this isn't possible currently on iOS unless you build your own custom tab bar, which isn't very difficult with storyboarding on iOS5.
Not possible ,create custome tab bar subclassing UITabbar
If you see this error: 'UITextAttributeTextShadowOffset' is deprecated: first deprecated in iOS 7.0 - Use NSShadowAttributeName with an NSShadow instance as the value.,try this.
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor grayColor];
shadow.shadowOffset = CGSizeMake(0.0, 0.5);
NSDictionary *attribute = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"AmericanTypewriter" size:10.0f], NSFontAttributeName,
[UIColor blackColor], NSForegroundColorAttributeName,
shadow,NSShadowAttributeName,nil];
[[UITabBarItem appearance] setTitleTextAttributes:attribute forState:UIControlStateNormal];
Try this.
[[UITabBarItem appearanceWhenContainedIn:[UITabBar class], nil]
setTitleTextAttributes:#{NSForegroundColorAttributeName:
[UIColor colorWithRed:0/255.0f green:130/255.0f blue:202/255.0f alpha:1.0],
NSFontAttributeName:[UIFont fontWithName:#"Signika-Semibold" size:20.0]
}
forState:UIControlStateNormal];

how to change front color of navigation bar title

I am using navigation controller in my application and want to change title color of navigationBar.
I am doing so using below code
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor grayColor],UITextAttributeTextColor,nil];
[self.navController.navigationBar setTitleTextAttributes:dic];
One more thing I am using ARC xcode 4.2 and this code is placed on appdelegate only
It is working fine in ios 4+
but not working on below versions.
Please help me how to do this from single code on appdelegate
I was just looking for the same thing and found Alex R. R.'s easy solution using iOS 5:
NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],UITextAttributeTextColor,
[UIColor blackColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(-1, 0)], UITextAttributeTextShadowOffset, nil];
[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];
Reference:
iPhone Navigation Bar Title text color
You can recreate the titleView like that in your viewcontroller:
UILabel * titleView = [[UILabel alloc] initWithFrame:CGRectZero];
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:20.0];
titleView.shadowColor = [UIColor colorWithWhite:1.0 alpha:1.0];
titleView.shadowOffset = CGSizeMake(0.0f, 1.0f);
titleView.textColor = [UIColor redColor]; // Your color here
self.navigationItem.titleView = titleView;
[titleView sizeToFit];
[titleView release];
the other parameters are those wich are used in the native titleView.
**
Please look at Max Strater's answer below to have a more recent solution.
**
After iOS 5, just do this :
nav1.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor];
In iOS 7, just use:
self.navController.navigationBar.titleTextAttributes = #{NSForegroundColorAttributeName : [UIColor whiteColor]};
Change [UIColor whiteColor] with whatever text color you want.
in iOS 7 this is how you can change the color of Navbar title/text:
UIColor *red = [UIColor colorWithRed:254.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:1.0];
NSMutableDictionary *navBarTextAttributes = [NSMutableDictionary dictionaryWithCapacity:1];
[navBarTextAttributes setObject:red forKey:NSForegroundColorAttributeName ];
self.navigationController.navigationBar.titleTextAttributes = navBarTextAttributes;

iPhone UINavigationBar change font style for all controllers with [UINavigationBar appearance]

I'm aware that I can individually change the font of a navigation bar as outlined in this answer: Change the navigation bar's font
Currently I'm using a more global approach:
//in my app delegate:
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlackTranslucent];
Is there a way to globally change the font that the Navbar through the appearance object?
thank you!
From Ray Wenderlich:
http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5
// Customize the title text for *all* UINavigationBars
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],
UITextAttributeTextColor,
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
UITextAttributeTextShadowOffset,
[UIFont fontWithName:#"Arial-Bold" size:0.0],
UITextAttributeFont,
nil]];
#Javy's answer with #Philip007's suggestion:
[[UINavigationBar appearance] setTitleTextAttributes: #{
UITextAttributeTextColor: [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],
UITextAttributeTextShadowColor: [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],
UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],
UITextAttributeFont: [UIFont fontWithName:#"Helvetica-Light" size:0.0f]
}];
ahh... that's better!
Above answers with updates for deprecated keys and use of NSShadow:
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor blackColor];
shadow.shadowBlurRadius = 0.0;
shadow.shadowOffset = CGSizeMake(0.0, 2.0);
[[UINavigationBar appearance] setTitleTextAttributes: #{
NSForegroundColorAttributeName : [UIColor blackColor],
NSFontAttributeName : [UIFont fontWithName:#"Helvetica-Light" size:0.0f],
NSShadowAttributeName : shadow
}];
Also setting the font size to 0 so it automatically resizes based on navigation bar orientation/height.

Using UIAppearance to change label height

Is there any way to use UIAppearance to change the height of a label inside of a UINavigationBar. Here's the code and an image of what's going on so you can understand what the problem is.
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-5.0 forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-5.0 forBarMetrics:UIBarMetricsLandscapePhone];
NSDictionary *textAttributes = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[UIFont fontWithName:#"MarketingScript" size:34.0], nil]
forKeys:[NSArray arrayWithObjects:UITextAttributeFont, nil]];
I ended up solving this by creating a custom UIView:
+(NavigationBarTitleLabel *)titleLabelWithTitle:(NSString *)title {
// this will appear as the title in the navigation bar
NavigationBarTitleLabel *labelWrapper = [[NavigationBarTitleLabel alloc] initWithFrame:CGRectMake(0, 0, 200, 34)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 8, 200, 32)];
label.font = [UIFont fontWithName:#"MarketingScript" size:28.0];
label.text = title;
label.textColor = XHEXCOLOR(0XFFFFFF);
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.layer.shadowColor = [UIColor blackColor].CGColor;
label.layer.shadowRadius = 2;
[labelWrapper addSubview:label];
[labelWrapper sizeToFit];
return labelWrapper;
}
and then setting the titleView property on the navigationItem:
self.navigationItem.titleView = [NavigationBarTitleLabel titleLabelWithTitle:self.title];
Probably you can get the UIAppearance for UILabel inside UINavigationBar.

How do I get the UIToolbar font to match UINavigation Controller?

Using the following post, I was able to add a UILabel to a UIToolbar, however, it looks crappy. Anyone know how to get the text size / color / shadow to match the title for a UINavigationController?
Navigation Controller
alt text http://www.codingwithoutcomments.com/uploads/Picture1.png
UIToolbar with UILabel
alt text http://www.codingwithoutcomments.com/uploads/Picture2.png
What steps do I need to take to make them match?
You might also want to add a shadow?
itemLabel.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
itemLabel.shadowOffset = CGSizeMake(0, -1.0);
Obviously the question is about iPhone, but in case anyone is wondering, here are the numbers for iPad:
titleLabel.font = [UIFont boldSystemFontOfSize:20.0];
titleLabel.shadowOffset = CGSizeMake(0, 1);
titleLabel.shadowColor = [UIColor colorWithWhite:1.f alpha:.5f];
titleLabel.textColor = [UIColor colorWithRed:113.f/255.f green:120.f/255.f blue:127.f/255.f alpha:1.f];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.text = #"Title";
UILabel * itemLabel = [[UILabel alloc] initWithFrame:rectArea];
itemLabel.text = #"Item";
itemLabel.textColor = [UIColor whiteColor];
itemLabel.font = [UIFont boldSystemFontOfSize:22.0];