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
Related
I am setting the title as follows, but I want to change the color of the text as well, but there is no textattributes, I wonder how it can be done ?
self.tabBarController?.navigationItem.title = "Settings"
I tried the following as well, but it did not even show the label.
let navLabel = UILabel()
let navTitle = NSMutableAttributedString(string: "Settings", attributes:[
NSAttributedString.Key.foregroundColor: UIColor.blue,
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17.0, weight: UIFont.Weight.light)])
navLabel.attributedText = navTitle
self.tabBarController?.navigationItem.titleView = navTitle
try this code, I hope it helps you.
Build Settings\Swift Language Version: 4.1
General\Deployment Target: 10.3
let attrsNormal = [NSAttributedStringKey.foregroundColor : UIColor.black,
NSAttributedStringKey.font : UIFont(name: "Arial", size: 14)!]
UITabBarItem.appearance().setTitleTextAttributes(attrsNormal,
for: UIControlState.normal)
The size of label is right ? Call fitThatSize
Swift 4, iOS 11.2.5 Xcode 9.2
Trying to change the font of the back button. Tried previous solutions found, but none seem to work under Swift 4, iOS 11.2.5 with my configuration, navigation controller within a tab bar controller.
Got this code, the first and last lines work, but the center three do not.
self.navigationController?.navigationBar.titleTextAttributes = [ NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!]
navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .normal)
navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .highlighted)
navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .focused)
navigationItem.title = "Filter \(titleSelection!) [shake to clear]"
This is in viewDidLoad method. Should this work?
For Swift 4, U can try this in AppDelegate.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red, NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 15)!], for: UIControlState.normal)
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.green, NSAttributedStringKey.font : UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)! ], for: .highlighted)
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.blue, NSAttributedStringKey.font : UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)! ], for: .focused)
return true
}
In ViewController.
override func viewWillAppear(_ animated: Bool) {
self.title = "List"
self.navigationController?.navigationBar.titleTextAttributes = [ NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!]
// THE BELOW THREE LINES NOT WORKING.
//navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .normal)
//navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .highlighted)
//navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .focused)
}
Storyboard
Output
You could change the font in the app delegate by doing something along these lines, this will change the font through out the whole app instead of in one viewcontroller though.
if let customFont = UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20.0) {
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: customFont], for: .normal)
}
To make it working on iOS 13.0 you should use UINavigationBarAppearience. Here is what you can do in order to change font of ALL elements in the navigation bar:
if #available(iOS 13.0, *) {
let navBarAppearance = UINavigationBarAppearance()
let attributes = [NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!]
navBarAppearance.titleTextAttributes = attributes
navBarAppearance.buttonAppearance.normal.titleTextAttributes = attributes
navBarAppearance.doneButtonAppearance.normal.titleTextAttributes = attributes
self.navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
self.navigationController?.navigationBar.standardAppearance = navBarAppearance
}
Instead of:
navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .normal)
Try:
self.navigationController?.navigationBar.topItem?.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .normal)
In Swift 5 you can do it by these:
let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17, weight: .regular)]
self.navigationItem.backBarButtonItem?.setTitleTextAttributes(attributes, for: .normal)
Please note it will be effective for the next pushed view controller not the current one on the display, that's why it's very confusing!
Also, check the storyboard and select the navigation item of the previous view controller then type something in the Back Button (Inspector).
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 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)
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)