I am trying to change font property for UINavigationItem.
I've tried using titleTextAttributes but somehow I am only able to change title font.
How can I change the font or UINavigationItem ? For example, the "More" text for the Back UIButton shown below:
I've tried using:
self.navigationController?.navigationBar.titleTextAttributes = [
NSForegroundColorAttributeName: UIColor.blackColor(),
NSFontAttributeName: UIFont(name: "Papyrus", size: 18)!
]
but it only changes what is shown on the picture, not the "More" Button's Font.
The reason your method wasn't working is because you were just using titleTextAttributes = ... when you have to use setTitleTextAttributes:forState: I use this function to customize the Nav Bar globally for my entire project:
func customizeNavBar(_ color: UIColor, titleFont: UIFont, buttonFont: UIFont) {
UINavigationBar.appearance().tintColor = color
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: color, NSFontAttributeName: titleFont]
UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: color, NSFontAttributeName: buttonFont], forState: UIControlState.Normal)
}
For a single instance, call the same functions:
someBarButton.tintColor.setTitleTextAttributes([NSForegroundColorAttributeName: color, NSFontAttributeName: buttonFont], forState: UIControlState.Normal)
Swift 4
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 15)!], for: .normal)
For iOS 13 and Swift 5.
For setting the bar button item text color & font:
UIBarButtonItem.appearance().setTitleTextAttributes([
.foregroundColor: UIColor.white,
.font: UIFont(name: GetFontNameBold(), size: 40)! ],
for: UIControl.State.normal)
For setting the title color & font just add in viewDidLoad() the following line:
UINavigationBar.appearance().titleTextAttributes = [
.foregroundColor: UIColor.white,
.font: UIFont(name: "Arial", size: 24)! ]
if you have outlet do this:
titleItem.titleLabel.font = UIFont(name: NAME, size: SIZE)
Related
I have a barbuttonItem here:
let doneBarButtonItem: UIBarButtonItem? = {
let barButtonItem = UIBarButtonItem()
barButtonItem.setTitleTextAttributes([
NSAttributedString.Key.font : UIFont(name: "Avenir-Heavy", size: 12)!,
NSAttributedString.Key.foregroundColor : UIColor.black,
], for: .normal)
barButtonItem.title = "DONE"
return barButtonItem
}()
Yet when i press on it, it changes to a different font. What is the property to change the font when highlighted? Thank you.
barButtonItem.setTitleTextAttributes([
NSAttributedString.Key.font : UIFont(name: "Avenir-Heavy", size: 12)!,
NSAttributedString.Key.foregroundColor : UIColor.black,
], for: .highlighted) // This line
What is the way to set custom font to left and right UIBarButtons of the UIImagePickerController?
I've tried this code but it doesn't work:
let imagePicker = UIImagePickerController()
imagePicker.navigationBar.barTintColor = UIColor(hex: 0x212121)
imagePicker.navigationBar.translucent = false
imagePicker.navigationBar.tintColor = UIColor.whiteColor()
imagePicker.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont(name: "Roboto-Regular", size: 20.0)!]
// doesn't work
imagePicker.navigationItem.backBarButtonItem?.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont(name: "Roboto-Regular", size: 25.0)!], forState: UIControlState.Normal)
imagePicker.navigationItem.leftBarButtonItem?.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont(name: "Roboto-Regular", size: 20.0)!], forState: UIControlState.Normal)
imagePicker.navigationItem.rightBarButtonItem?.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont(name: "Roboto-Regular", size: 20.0)!], forState: UIControlState.Normal)
you are going to need to subclass UIImagePickerController and set your own tool bar.
There is a great example from apple: https://developer.apple.com/library/ios/samplecode/PhotoPicker/Introduction/Intro.html
It's in objective-C but I'm sure it will give you an idea of what you need to do.
I want to be change the font of the title to Avenir, and I want to make it white. Here is my code in the viewDidLoad method:
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "Avenir", size: 20)!]
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
This code only changes the color of the title, not the font. Additionally, I tried implementing this in the App Delegate File, but that did not work.
Here is what my UINavigationBar looks like:
I want the title to have a font of Avenir as well. How do I achieve this?
It seems this is needed as well:
self.navigationController?.navigationBar.barStyle = UIBarStyle.Black // I then set the color using:
self.navigationController?.navigationBar.barTintColor = UIColor(red: 204/255, green: 47/255, blue: 40/255, alpha: 1.0) // a lovely red
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor() // for titles, buttons, etc.
let navigationTitleFont = UIFont(name: "Avenir", size: 20)!
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: navigationTitleFont]
If you want the styles to be applied through out the whole app add these lines in the AppDelegate.m file.
NSShadow* shadow = [NSShadow new];
shadow.shadowOffset = CGSizeMake(0.0f, 0.0f);
shadow.shadowColor = [UIColor blackColor];
[[UINavigationBar appearance] setTitleTextAttributes: #{
NSForegroundColorAttributeName: [UIColor whiteColor],
NSFontAttributeName: [UIFont fontWithName:#"Kelvetica Nobis" size:20.0f],
NSShadowAttributeName: shadow
}];
NSForegroundColorAttributeName sets the Font color.
NSFontAttributeName sets a custom font face to the title text.
NSShadowAttributeName applies a nice shadow effect to the text, you can remove this part if you want.
Also in advance, you can add these line to hide the text that comes up in back button in action bar when you navigate to another view within the navigation controller.
[[UIBarButtonItem appearance]
setBackButtonTitlePositionAdjustment:UIOffsetMake(-1000, -1000)
forBarMetrics:UIBarMetricsDefault];
Hope this help your question :)
In case someone needs this in Swift 4:
let navBarAppearance = UINavigationBar.appearance()
navBarAppearance.barTintColor = .red
navBarAppearance.tintColor = .white
navBarAppearance.titleTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.font: UIFont(name: "Avenir", size: 20)!]
Your code is OK you just need to set all of titleTextAttributes in one line:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white , NSFontAttributeName: UIFont(name: "Avenir", size: 17)!]
In Swift 5:
let appearance = UINavigationBarAppearance()
appearance.titleTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.font: UIFont(name: "Avenir", size: 20)!]
appearance.largeTitleTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.font: UIFont(name: "Avenir", size: 35)!]
Code here are for Title and Large title. you can also remove custom font if you want system font.
I'm updating this for iOS 15. If you have a scroll view that pushes something behind the Nav Bar it WILL change the NavigationBar background if you do NOT set "navigationBar.scrollEdgeAppearance".
So here is a simple config to set it for ALL of your views in ViewDidLoad of your main ViewController.
// Set top Nav Bar behavior for ALL of app
let standardAppearance = UINavigationBarAppearance()
// Title font color
standardAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
// prevent Nav Bar color change on scroll view push behind NavBar
standardAppearance.configureWithOpaqueBackground()
standardAppearance.backgroundColor = UIColor.blue
self.navigationController?.navigationBar.standardAppearance = standardAppearance
self.navigationController?.navigationBar.scrollEdgeAppearance = standardAppearance
So the 'scrollEdgeAppearance' you can set title color, tint, NavBar color and the works. The standardAppearance is what shows at loading.
Took me a while to figure this one out for iOS15.
You're welcome! :-)
I would just create an outlet and do this:
#IBOutlet weak var navigationBar: UINavigationBar!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Avenir", size: 30)!]
}
The following code was running fine before updating to Xcode 6.1 to stay up with iOS 8.1:
override func viewDidAppear(animated: Bool) {
self.navigationController?.navigationBar.topItem?.title = "Home"
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34), NSForegroundColorAttributeName: UIColor.whiteColor()]
}
The issue is specifically in NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34)
and the error I'm getting here is:
! "Could not find an overload for 'init' that accepts the supplied arguments"
I found this original code on a different StackOverflow question and it was working as expected until this update (downloaded it yesterday). My font is indeed installed properly.
Should I be writing this code differently now, or is there an entirely new way in which I should set my Navigation Bar Font?
Thanks!
Whoops. I figured this out on my own:
I needed an exclamation point following my declaration of the NSFontAttributeName as it requires a type "NSString!". Perhaps it only required an "NSString" before, but I have no issues now.
Working line:
NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 24)!
Working full code:
override func viewDidAppear(animated: Bool) {
self.navigationController?.navigationBar.topItem?.title = "Home"
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34)!, NSForegroundColorAttributeName: UIColor.whiteColor()]
}
Seems like a silly question now. Hope this helps someone else!
You are using UIFont(name: intializer as it is defined as
init?(name fontName: String, size fontSize: CGFloat) -> UIFont
failable intializer read more from link.So it is returning optional.You need to unwrap it as it require AnyObject not optional.
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34)!, NSForegroundColorAttributeName: UIColor.whiteColor()]
Thanks Benji!!!
I changed it a bit and applied it to the appearance attribute of the navigation controller.
var navigationBarAppearance = UINavigationBar.appearance()
navigationBarAppearance.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "OpenSans", size: 16)!, NSForegroundColorAttributeName: UIColor.whiteColor()]
It is better to declare your font as conditional , like this way :
if let font = UIFont (name: "AdobeArabic-BoldItalic", size: 20) {
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: font]
}
Doing this , makes sure that your font is already found , that I have installed new font , and when used it without conditional if , it issued an exception , of unwrapping an optional .
override func viewWillLayoutSubviews() {
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName : UIFont(name: "Roboto-Regular", size: 14)!]
}
Swift 4.2
Change Small Title when layout scrolled down
override func viewDidAppear(animated: Bool) {
self.navigationController?.navigationBar.topItem?.title = "Home"
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34), NSForegroundColorAttributeName: UIColor.whiteColor()]
}
Change Large Title
override func viewDidAppear(animated: Bool) {
self.navigationController?.navigationBar.topItem?.title = "Home"
self.navigationController?.navigationBar.largeTitleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34), NSForegroundColorAttributeName: UIColor.whiteColor()]
}
Has anyone tried using the appearance proxy in swift yet?
This syntax doesn't work, has anyone figured out how to set title text attributes on controls like segmentedControl or UITabBar? I think I am close
segmentedControl.titleTextAttributesForState(UIControlState.Normal) =
NSDictionary(objects: [UIFont(name: fontFamilyRegular, size: 16.0)],
forKeys: [NSFontAttributeName])
This should do it:
segmentedControl.setTitleTextAttributes([
NSFontAttributeName: UIFont(name: "Helvetica", size: 16.0)!,
NSForegroundColorAttributeName: UIColor.blueColor()
], forState: UIControlState.Normal)
Make sure you unwrap the the font (!)
let font = UIFont(name: "HelveticaNeue-Light", size:15.0)!
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName:font,NSForegroundColorAttributeName:UIColor.redColor()], forState: UIControlState.Normal)
For XCode 6.1, try this:
UITabBarItem.appearance().setTitleTextAttributes(NSDictionary(object: UIFont(name: "Helvetica", size: 16.0)!, forKey: NSFontAttributeName), forState: UIControlState.Normal)