UINavigationBar: Changing the font programmatically - ios

I'm implementing a UINavigationBar with some text on it but I want to change the font in the UINavigationBar text any of you knows if is posible to change font in the text programmatically?
I'll really appreciate your help

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
}];

Related

Change UI color globally(Swift)

I want to do something like this:
In Setting of app, can chose a color, after that some UI element like Navigationbar, tabbar highlight will change to that color.
Is there anyway of tut for that?
Here's how you'd do it in Objective-C.
- (void)setCustomizedNavigationBarStyle {
// UIBarButtonItem styling
NSShadow *shadow = [[NSShadow alloc]init];
shadow.shadowOffset = CGSizeMake(0.0, 1.0);
shadow.shadowColor = [UIColor clearColor];
NSDictionary *enabledTextAttributeDictionary = #{NSForegroundColorAttributeName : [UIColor darkGrayColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:#"GillSans" size:17.0]};
[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:#[[UINavigationBar class]]] setTitleTextAttributes:enabledTextAttributeDictionary forState:UIControlStateNormal];
NSDictionary *disabledTextAttributeDictionary = #{NSForegroundColorAttributeName : [UIColor lightGrayColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:#"GillSans" size:17.0]};
[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:#[[UINavigationBar class]]] setTitleTextAttributes:disabledTextAttributeDictionary forState:UIControlStateDisabled];
// UINavigationBarTitle styling
NSDictionary *titleAttributeDictionary = #{NSForegroundColorAttributeName : [UIColor blackColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:#"GillSans" size:18.0]};
[[UINavigationBar appearanceWhenContainedInInstancesOfClasses:#[[UINavigationController class]]]setTitleTextAttributes:titleAttributeDictionary];
}
You could call this in didFinishLaunchingWithOptions. Once you translate to Swift, you'd add this line to didFinishLaunchingWithOptions:
setCustomizedNavigationBarStyle()
This should be readily translatable into Swift.
Adding to this, you can create a custom palette of colors. You may find this post on the topic helpful:
How do I create a category in Xcode 6 or higher?
You could save the color in NSUserDefaults and retrieve it by the key whenever you need to set that color to your view elements. You'll need an extension to NSUserDefaults that returns an UIColor.
Check out the accepted answer to this question.
Hope it helps!

UINavigationBar back button item font color won't set

I have a navigation bar on a viewController that I can enable/disable. The problem is I can't get the font for the UIBarButtonItems to change colors after the view initially loads, though the back arrow will change.
I disable the UINavigationBar on myViewController with the following line of code:
self.navigationController.navigationBar.userInteractionEnabled = NO;
I have UIControlStates configured in AppDelegate.m for UIBarButtonItems for enabled and disabled, but nothing happens to the font after the view initially loads.
In my AppDelegate.m, I have the following code to set the UINavigationBar's font and color:
// UIBarButtonItem styling
NSShadow *shadow = [[NSShadow alloc]init];
shadow.shadowOffset = CGSizeMake(0.0, 1.0);
shadow.shadowColor = [UIColor whiteColor];
// enabled
NSDictionary *enabledTextAttributeDictionary = #{NSForegroundColorAttributeName : [UIColor customCellHyperlinkColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:#"GillSans" size:17.0]};
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:enabledTextAttributeDictionary forState:UIControlStateNormal];
// disabled
NSDictionary *disabledTextAttributeDictionary = #{NSForegroundColorAttributeName : [UIColor lightGrayColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:#"GillSans" size:17.0]};
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:disabledTextAttributeDictionary forState:UIControlStateDisabled];
// UINavigationBarTitle styling
NSDictionary *titleAttributeDictionary = #{NSForegroundColorAttributeName : [UIColor blackColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:#"GillSans" size:19.0]};
[[UINavigationBar appearanceWhenContainedIn:[UINavigationController class], nil]setTitleTextAttributes:titleAttributeDictionary];
I thought since I configured the 2 states (UIControlStateNormal/UIControlStateDisabled) in AppDelegate.m, nothing needs to be done other than enable/disable the navigationBar. Enable/disable does indeed enable/disable, but the color on the back button label doesn't change (though it does initially set to the color I set for UIControlStateNormal in AppDelegate).
I tried to manually set it, but the label on the backButtonItem stays blue while the icon to the left of it tints light gray. What (obvious thing) am I missing that is preventing me from changing the color of my back button font?
I think you are looking for this
Add this code in you AppDelegate.m
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowOffset = CGSizeMake(0.0, 1.0);
shadow.shadowColor = [UIColor clearColor];
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
setTitleTextAttributes:
#{NSForegroundColorAttributeName:[UIColor redColor],
NSShadowAttributeName:shadow,
NSFontAttributeName:[UIFont systemFontOfSize:13.0]
}
forState:UIControlStateNormal];
I think you need to use
[self.navigationController.navigationBar setBarTintColor:[UIColor lightGrayColor]];
self.navigationController.navigationBar.tintColor = [UIColor lightGrayColor];
[self.navigationController.navigationBar setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor lightGrayColor], NSFontAttributeName : place your font here}];
instead of
self.navigationController.navigationBar.tintColor = [UIColor lightGrayColor];

Is there a way to change title in navigation bar to Italic, Bold and underlined without changing font?

I use the UIAppearance to change attributes of my title in Navigation Bar like so:
[[UINavigationBar appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [MM mainTitleColor]}];
But I have not found the way to make text underlined or italic, is there a way to do this without changing the font alltogether?
No. These properties you cant change unless font changing. Because the available keys under the appearance proxy are
UITextAttributeFont
UITextAttributeTextColor
UITextAttributeTextShadowColor
UITextAttributeTextShadowOffset
Change these properties to customize the UINavigationBar
If you looking for font change, then see the below example
[[UINavigationBar appearance] setTitleTextAttributes:#{
UITextAttributeTextColor: TEXT_COLOR,
UITextAttributeTextShadowColor: SHADOW_COLOR,
UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
UITextAttributeFont: [UIFont fontWithName:#"Arial-Bold" size:0.0],
}];
Try this one.
You have to customise your navigation controller with adding a imageview.
UIFont *yourFont = [UIFont fontWithName:#"Helvetica-BoldOblique" size:[UIFont systemFontSize]];
if ([[UINavigationBar class]respondsToSelector:#selector(appearance)]) {
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"myimage.png"] forBarMetrics:UIBarMetricsDefault];
}
[[UINavigationBar appearance] setTitleTextAttributes:#{
UITextAttributeTextColor : [UIColor clearColor],
UITextAttributeTextShadowColor : [UIColor blackColor],
UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetMake(1, 0)],
UITextAttributeFont : yourFont
}];
You can use :
Optima-BoldItalic
TimesNewRomanPS-BoldItalicMT
Baskerville-BoldItalic
HelveticaNeue-BoldItalic
TrebuchetMS-Bold
Helvetica-BoldOblique
You can try below code to make your text italic and bold.
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
shadow.shadowOffset = CGSizeMake(0, 1);
[[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-BoldItalic" size:21.0], NSFontAttributeName, nil]];
[self setTitle:#"Title text"];

UISegmentedControl setTitleTextAttributes:forState: not working well with font

I have a UISegmentedControl that I'm trying to customize, and I'm having two problems :
This is my code :
NSDictionary *attributes = #{UITextAttributeFont: [UIFont fontWithName:#"Gotham-Book" size:14.f],
UITextAttributeTextColor: [UIColor whiteColor],
UITextAttributeTextShadowColor: [UIColor clearColor]};
[segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal];
NSDictionary *selectedAttributes = #{UITextAttributeFont: [UIFont fontWithName:#"Gotham-Medium" size:14.f],
UITextAttributeTextColor: [UIColor redColor]};
[segmentedControl setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];
1) The UISegmentedControl reacts well for the UIControlStateNormal but for the forState:UIControlStateSelected, the color works but not the font. FYI, the font exists because if I switch "Gotham-Book" and "Gotham-Medium" for UIControlStateNormal, I see the difference.
2) Before I've set the font for the UIControlStateNormal, the text was vertically aligned, but now it's not anymore. How could I change the textInsets ?
Thank's for your help !
On IOS 7 use NSFontAttributeName, NSForegroundColorAttributeName and NSShadowAttributeName (if needed),
For backward compatibility I using both variants, like:
NSDictionary *attributes = #{UITextAttributeFont: [UIFont fontWithName:#"Gotham-Book" size:14.f],
UITextAttributeTextColor: [UIColor whiteColor],
UITextAttributeTextShadowColor: [UIColor clearColor],
NSFontAttributeName: [UIFont fontWithName:#"Gotham-Book" size:14.f],
NSForegroundColorAttributeName: [UIColor whiteColor],
};
[segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal];

iOS - Globally change navigation bar title color using appearance?

This crashes the app:
[[UINavigationBar appearance] setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
Is there a way to do this using appearance?
This worked:
NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor darkGrayColor], UITextAttributeTextColor, [UIColor whiteColor], UITextAttributeTextShadowColor, nil];
[[UINavigationBar appearance] setTitleTextAttributes:textTitleOptions];
Here's an example of how to do this in Swift:
UINavigationBar.appearance().titleTextAttributes =
[NSFontAttributeName:UIFont(name:"Exo2-Bold", size: 18) as! AnyObject,
NSForegroundColorAttributeName:UIColor.whiteColor()]
That crashes the app before UINavigationBar doesn't have a title or state... Those are UIButton methods
You need
[[UINavigationBar appearance] setTintColor:[UIColor darkGrayColor]];
The #RyJ answer is great and worked for me. Thought I'd chip in that there's a good tutorial on this in Ray Wenderlich's site, titled (excuse the pun):
User Interface Customization in iOS 6
See the section Customizing UINavigationBar
Here's the code snippet for the navigation bar title, to change globally:
// 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]];
One other minor point is that it seems there's a default shadow on the title bar, so to get rid of it, you can't just remove the attribute. Instead you have to set a shadow offset:
UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetMake(0, 0)]
for iOS 15
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = <PREFERRED BACKGROUND COLOR>
appearance.titleTextAttributes = [.foregroundColor : <PREFERRED TITLE COLOR>]
navigationBar.tintColor = <PREFERED TINT COLOR> //for bar buttons
navigationBar.standardAppearance = appearance;
navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance
I used following code to change the title bar's color.
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor blackColor];
shadow.shadowOffset = CGSizeMake(1, 0);
NSDictionary *titleTextAttributes = #{NSForegroundColorAttributeName:[UIColor whiteColor],
NSShadowAttributeName:shadow};
[[UINavigationBar appearance] setTitleTextAttributes:titleTextAttributes];
Using modern syntax and code that actually runs, this is how to globally style your UINavigationBar title text:
NSShadow *navigationBarTitleShadow = [[NSShadow alloc] init];
navigationBarTitleShadow.shadowColor = [UIColor colorWithWhite:0.5
alpha:0.5];
navigationBarTitleShadow.shadowOffset = CGSizeMake(2.0, 2.0);
[[UINavigationBar appearance] setTitleTextAttributes:#{ NSForegroundColorAttributeName : [UIColor blackColor],
NSFontAttributeName : [UIFont fontWithName:#"Arial-BoldMT"
size:30.0],
NSShadowAttributeName : navigationBarTitleShadow }];
Note: NSShadow's shadowBlurRadius property is not respected.
Note: Shadows are so iOS 6. Don't ever use them.

Resources