How to set navigationbar font and colors - ios

I am trying to set the color of my navigation bar. I have added my font in the plist file and added it as a resource, and I can select it as font in the storyboard when using a UILabel. So the font seems to be there.
I have tried this in the app delegate:
NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:myLightBrownColor, NSForegroundColorAttributeName,[UIFont fontWithName:#"Pacifico" size:17.0f], NSFontAttributeName,
myShadow, NSShadowAttributeName, nil];
[navigationBarAppearance setTitleTextAttributes:textTitleOptions];
And this in the view controller directly:
NSShadow *myShadow = [MRStyleController getMyShadow];
NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:[MRStyleController getLightBrownColor], NSForegroundColorAttributeName,[UIFont fontWithName:#"Pacifico" size:17.0f], NSFontAttributeName,
myShadow, NSShadowAttributeName, nil];
self.navigationController.navigationBar.titleTextAttributes = textTitleOptions;
However, none of this did change the apps navigation bar title font and color.
Any suggestions?
EDIT:
what correctly works is:
UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];
[navigationBarAppearance setBarTintColor:myBrownColor];
NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:myLightBrownColor, NSForegroundColorAttributeName,
myShadow, NSShadowAttributeName,
[UIFont fontWithName:#"AmericanTypewriter" size:16.0], NSFontAttributeName, nil];
[navigationBarAppearance setTitleTextAttributes:textTitleOptions];
This will change the font to "American TypeWriter".
But I have a custom font named "Pacifico", which is in my "Supporting Files" folder, as "Pacifico.ttf", and listed in my info.plist as "Fonts provided by application", but the font isn't changing, so somehow the custom font is not correctly referenced?

For navigation bar title use
self.navigationController.navigationBar.titleTextAttributes= #{
NSForegroundColorAttributeName: [UIColor purpleColor],
NSFontAttributeName: [UIFont robotoMediumFontSize : 20.0],
};
self.navigationItem.title=#"MY Title";
For setting navigation bar background color use
self.navigationController.navigationBar.barTintColor=[UIColor myColor];

I found the solution, the font needs to be added in the info.plist as described above, but additionally in ---> Build Phases ---> Copy Bundle Resources added.

Related

iOS UISegmentedControl opens sans font not applying?

I try to apply opensans-regular font to segmented control title. But it is behaving Abnormal. Can't we apply opensans font to UISegmentedControl?
Here is my code
hmSegmentControl.titleTextAttributes = #{NSForegroundColorAttributeName : [UIColor redColor],NSFontAttributeName : [UIFont fontWithName:#"OpenSans-Regular" size:13]};
hmSegmentControl.selectedTitleTextAttributes = #{NSForegroundColorAttributeName : [UIColor blackColor],NSFontAttributeName : [UIFont fontWithName:#"OpenSans-Regular" size:13]};
Issue got fixed. For OpenSans-Regular font, we have to give font name as #"OpenSans" But not #"OpenSans-Regular".

FontAwesome for UITTabBarController

I am working on building a mobile app to compliment a website and I need to mimic the icons used on the site. The icons are from FontAwesome and I want them to appear as my UITabBar Items. I have successfully installed the FontAwesome into my project and then I was able to add the icon into a label, but I can't figure out how to get the icon to render in my tab bar item.
The code I used to render the icon in the label:
self.label.font = [UIFont fontWithName:#"FontAwesome" size:12];
self.label.text = [NSString stringWithFormat:#"%#", #"\uf007"];
I have also tried typing in "\uf007" into the title of the icon, and then changing the font in my viewDidLoad method, but that did not work. Thank you for any help!
EDIT
My reputation is below 10 so the link to the image is: uitabbaritem
In the image above, I am manually typing in the code I want to show. If I change the font to FontAwesome, the text stays the same.
[[UITabBarItem appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor], UITextAttributeTextColor,
[UIFont fontWithName:#"font" size:0.0], UITextAttributeFont,
nil]
forState:UIControlStateHighlighted];
Reference - iOS5 TabBar Fonts and Color

How do I set the size of the text I'm drawing here?

I'm drawing some text using the code below. How do I set the size of this text?
NSDictionary *attrs =
#{ NSForegroundColorAttributeName : [UIColor grayColor],
};
NSString *currentRank = #"Sample text";
[currentRank drawAtPoint:CGPointMake(100, 100) withAttributes:attrs];
Looking at the docs it seems as though the font name contains the size
NSFontAttributeName
NSFont
Default Helvetica 12-point
Available in OS X v10.0 and later.
Declared in NSAttributedString.h.

iOS change navigation bar title font and color

So i have this code that should change the nav bar title font, but it doenst
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont
fontWithName:_dataManager.optionsSettings.fontString size:14], NSFontAttributeName,
[UIColor whiteColor], NSForegroundColorAttributeName, nil];
[[UINavigationBar appearance] setTitleTextAttributes:attributes];
Changing the back button font with this code works just fine.
//set backbutton font
NSDictionary *normalAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:_dataManager.optionsSettings.fontString size:15], NSFontAttributeName,
nil];
[[UIBarButtonItem appearance] setTitleTextAttributes:normalAttributes
forState:UIControlStateNormal];
The correct way to change the title font (and color) is:
[self.navigationController.navigationBar setTitleTextAttributes:
#{NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:[UIFont fontWithName:#"mplus-1c-regular" size:21]}];
Edit: Swift 4.2
self.navigationController?.navigationBar.titleTextAttributes =
[NSAttributedString.Key.foregroundColor: UIColor.red,
NSAttributedString.Key.font: UIFont(name: "mplus-1c-regular", size: 21)!]
Edit: Swift 4
self.navigationController?.navigationBar.titleTextAttributes =
[NSAttributedStringKey.foregroundColor: UIColor.red,
NSAttributedStringKey.font: UIFont(name: "mplus-1c-regular", size: 21)!]
Swift 3:
self.navigationController?.navigationBar.titleTextAttributes =
[NSForegroundColorAttributeName: UIColor.redColor(),
NSFontAttributeName: UIFont(name: "mplus-1c-regular", size: 21)!]
Swift 5:
navigation.navigationBar.titleTextAttributes = [
.foregroundColor: UIColor.red,
.font: UIFont(name: "mplus-1c-regular", size: 21)!
]
There is nothing wrong with the other answers. I'm just sharing the storyboard version for setting the font.
1. Select Your Navigation Bar within your Navigation Controller
2. Change the Title Font in the Attributes Inspector
(You will likely need to toggle the Bar Tint for the Navigation Bar before Xcode picks up the new font)
Notes (Caveats)
Verified that this does work on Xcode 7.1.1+. (See the Samples below)
You do need to toggle the nav bar tint before the font takes effect (seems like a bug in Xcode; you can switch it back to default and font will stick)
If you choose a system font ~ Be sure to make sure the size is not
0.0 (Otherwise the new font will be ignored)
Seems like this works with no problem when only one NavBar is in the view
hierarchy. It appears that secondary NavBars in the same stack are ignored. (Note that if you show the master navigation controller's navBar all the other custom navBar settings are ignored).
Gotchas (deux)
Some of these are repeated which means they are very likely worth noting.
Sometimes the storyboard xml gets corrupt. This requires you to
review the structure in Storyboard as Source Code mode (right click
the storyboard file > Open As ...)
In some cases the navigationItem tag associated with user defined runtime attribute was set as an xml
child of the view tag instead of the view controller tag. If so
remove it from between the tags for proper operation.
Toggle the NavBar Tint to ensure the custom font is
used.
Verify the size parameter of the font unless using a dynamic font
style
View hierarchy will override the settings. It appears that one font
per stack is possible.
Result
Samples
Video Showing Multiple Fonts In Advanced Project
Simple Source Download
Advanced Project Download ~ Shows Multiple NavBar Fonts & Custom Font Workaround
Video Showing Multiple Fonts & Custom Fonts
Handling Custom Fonts
Note ~ A nice checklist can be found from the Code With Chris website and you can see the sample download project.
If you have your own font and want to use that in your storyboard, then there is a decent set of answers on the following SO Question. One answer identifies these steps.
Get you custom font file(.ttf,.ttc)
Import the font files to your Xcode project
In the app-info.plist,add a key named Fonts provided by
application.It's an array type , add all your font file names to the
array,note:including the file extension.
In the storyboard , on the NavigationBar go to the Attribute
Inspector,click the right icon button of the Font select area.In the
popup panel , choose Font to Custom, and choose the Family of you
embeded font name.
Custom Font Workaround
So Xcode naturally looks like it can handle custom fonts on UINavigationItem but that feature is just not updating properly (The font selected is ignored).
To workaround this:
One way is to fix using the storyboard and adding a line of
code: First add a UIView (UIButton, UILabel, or some other UIView
subclass) to the View Controller (Not the Navigation Item...Xcode is not currently allowing one to do that). After you add the control
you can modify the font in the storyboard and add a reference as an
outlet to your View Controller. Just assign that view to the
UINavigationItem.titleView. You could also set the text name in code
if necessary. Reported Bug (23600285).
#IBOutlet var customFontTitleView: UIButton!
//Sometime later...
self.navigationItem.titleView = customFontTitleView
Try this:
NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],NSForegroundColorAttributeName,
[UIColor whiteColor],NSBackgroundColorAttributeName,nil];
self.navigationController.navigationBar.titleTextAttributes = textAttributes;
My Swift code for change Navigation Bar title:
let attributes = [NSFontAttributeName : UIFont(name: "Roboto-Medium", size: 16)!, NSForegroundColorAttributeName : UIColor.whiteColor()]
self.navigationController.navigationBar.titleTextAttributes = attributes
And if you want to change background font too then I have this in my AppDelegate:
let attributes = [NSFontAttributeName : UIFont(name: "Roboto-Medium", size: 16)!, NSForegroundColorAttributeName : UIColor.whiteColor()]
UIBarButtonItem.appearance().setTitleTextAttributes(attributes, forState: UIControlState.Normal)
ADD this single line code in your App Delegate - Did Finish Lauch. It will change Font, color of navigation bar throughout the application.
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font: UIFont(name: "YOUR FONT NAME", size: 25.0)!]
Here is an answer for your question:
Move your code to below method because navigation bar title updated after view loaded. I tried adding above code in viewDidLoad doesn't work, it works fine in viewDidAppear method.
-(void)viewDidAppear:(BOOL)animated{}
Anyone needs a Swift 3 version. redColor() has changed to just red.
self.navigationController?.navigationBar.titleTextAttributes =
[NSForegroundColorAttributeName: UIColor.red,
NSFontAttributeName: UIFont(name: "{your-font-name}", size: 21)!]
It's a bit more readable using literals:
self.navigationController.navigationBar.titleTextAttributes = #{
NSFontAttributeName:[UIFont fontWithName:#"mplus-1c-regular" size:21],
NSForegroundColorAttributeName: [UIColor whiteColor]
};
I had one problem because when I tried to change my NavigationItem title programmatically i was not able to find my family font (tried many things but impossible to make it run correctly) so I found one workaround very nice and easy in storyboard.
Firstly you add under Navigation Item one view in middle and don't forget to set backGroundColor to clear color to have the same color of navBar:
Then you add one Label which you can edit (set color of text, font, size...) in this view
You add constraints to label (Top = 0, Bottom = 0, Trailing = 0 and Leading = 0) to View and center text of label
Finally you should have something like that in document outline:
And something like that in your ViewController:
Hope it can help.
iOS 11
Objective-C
if (#available(iOS 11.0, *)) {
self.navigationController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
self.navigationController.navigationBar.prefersLargeTitles = true;
// Change Color
self.navigationController.navigationBar.largeTitleTextAttributes = #{NSForegroundColorAttributeName: [UIColor whiteColor]};
} else {
// Fallback on earlier versions
}
Swift:-
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont(name:"Love Nature", size: 40)!]
This one for alternative to Swift 4 (already answer by #Josh):
let titleTextAttributed: [NSAttributedStringKey: Any] = [.foregroundColor: UIColor.red, .font: UIFont(name: "AvenirNext-Regular", size: 20) as Any]
navigationController?.navigationBar.titleTextAttributes = titleTextAttributed
For Objective-C to set Font and Color
- (void)_setup {
NSDictionary *barButtonTitleAttributes = #{
NSForegroundColorAttributeName : [UIColor whiteColor],
NSFontAttributeName :[UIFont fontWithName:#"Lato-Regular" size:15.0]
};
[self.navigationBar setTitleTextAttributes:barButtonTitleAttributes];
}
Here is an answer for Swift 4 😁:
let textAttributes:[String:Any]? = [NSAttributedStringKey.foregroundColor.rawValue:UIColor.blue, NSAttributedStringKey.font.rawValue:UIFont(name:"Avenir Next", size:20)!]
navigationController?.navigationBar.titleTextAttributes = textAttributes
Don't forget to add the Raw values of the keys to avoid compile errors.
let textAttributes:[NSAttributedStringKey: Any] = [NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue):UIColor.blue, NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue):UIFont(name:"OpenSans", size: 17)!]
navigationController?.navigationBar.titleTextAttributes = textAttributes
Swift 4.2
self.navigationController?.navigationBar.titleTextAttributes =
[NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.font: UIFont(name: "LemonMilklight", size: 21)!]
Add this extension
extension UINavigationBar {
func changeFont() {
self.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont(name:"Poppins-Medium", size: 17)!]
}
}
Add the following line in viewDidLoad()
self.navigationController?.navigationBar.changeFont()
Working in swift 3.0
For changing the title color you need to add titleTextAttributes like this
let textAttributes = [NSForegroundColorAttributeName:UIColor.white]
self.navigationController.navigationBar.titleTextAttributes = textAttributes
For changing navigationBar background color you can use this
self.navigationController.navigationBar.barTintColor = UIColor.white
For changing navigationBar back title and back arrow color you can use this
self.navigationController.navigationBar.tintColor = UIColor.white

In iOS7, UIBarButtonItems do not respect bold "Done" styling when using UIAppearance proxy

In iOS7, by default UIBarButtonItem uses a Helvetica regular weight font for style UIBarButtonItemStylePlain and a bold weight for UIBarButtonItemStyleDone.
My app uses custom fonts, and I'm using a UIAppearance proxy to achieve this:
appearance = #{NSFontAttributeName: [UIFont fontWithName:#"ProximaNova-Regular" size:18.0]};
[[UIBarButtonItem appearance] setTitleTextAttributes:appearance
forState:UIControlStateNormal];
The trouble is, the appearance proxy makes the Plain and Done styled buttons the regular weight font I specified above.
Any ideas how I could get UIBarButtonItem to use different custom font weights depending on the style?
I know it is late answer, but it can be helpful for somebody:
UIBarButtonItem *customBarButton =
[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"CustomTitle", #"This button appears in my smexy ViewController's naviagtion bar")
style:UIBarButtonItemStylePlain
target:self
action:#selector(customButtonDidClick:)];
NSDictionary *attributes = #{NSFontAttributeName: [UIFont fontWithName:#"TimesNewRomanPS-BoldMT" size:14.0f],
NSForegroundColorAttributeName: [UIColor redColor]}; // here you can add some other keys (especially in iOS 7) to personalize your button title more
[customBarButton setTitleTextAttributes:attributes forState:UIControlStateNormal];
[self.navigationItem setRightBarButtonItem:customBarButton];
Edited: thanks for detection of my typo :-)

Resources