left and right barButtons font in UIImagePickerController - ios

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.

Related

Font of barButtonItem changes when highlighted swift 4.2

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

Change the font on the back button of a navigation controller under swift 4

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).

How to change UINavigationItem font?

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)

Error when instantiating a UIFont in an text attributes dictionary

I'm trying to set the font of the UIBarButtonItem like so:
let barButton = UIBarButtonItem.appearance()
barButton.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "AvenirNext", size: 15], forState: UIControlState.Normal)
But it throws a compiler error saying:
Cannot invoke 'init' with an argument list type '($T7, forState: UIControlState)`
and I have no idea what that means. I have also tried
barButton.titleTextAttributesForState(UIControlState.Normal) =[NSFontAttributeName...]`
but it appears that it isn't assignable
How can I resolve this?
The initializer of UIFont returns an optional because it may fail due to misspelled font name etc.
You have to unwrap it and check:
if let font = UIFont(name: "AvenirNext", size: 15) {
barButton.setTitleTextAttributes([NSFontAttributeName: font], forState: UIControlState.Normal)
}
UPDATED for Swift 3
if let font = UIFont(name: "AvenirNext", size: 15) {
barButton.setTitleTextAttributes([NSFontAttributeName:font], for: .normal)
}
Setting Custom font is little bit tricky, since they don't have font and title properties. Hope this following answer will help you.
let font = UIFont(name: "<your_custom_font_name>", size: <font_size>)
var leftBarButtonItem = UIBarButtonItem(title: "<font_hex_code>", style: UIBarButtonStyle.Plain, target: self, action: "buttonClicked:")
leftBarButtonItem.setTitleTextAttributes([NSFontAttributeName:font!], forState: UIControlState.Normal)
self.navigationItem.leftBarButtonItem = leftBarButtonItem
if let font : UIFont = UIFont(name: "Roboto-Regular", size: 15)
{
cancelBarButton.setTitleTextAttributes([NSFontAttributeName: font], forState: UIControlState.Normal)
doneBarButton.setTitleTextAttributes([NSFontAttributeName: font], forState: UIControlState.Normal)
}
With Swift 4
NSFontAttributeName is deprecated, you can use NSAttributedStringKey values to set attributes.
if let fontStyle = UIFont(name: "HelveticaNeue-Light", size: 19) {
navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: fontStyle]
}

Appearance Proxy in Swift (iOS)

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)

Resources