Cannot Set Navigation Bar Font in Swift on Latest Xcode Version - ios

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()]
}

Related

UITextField Placeholder and Text Size should be separate - Swift

My requirement is such that when placeholder of textfield is visible then its color should be gray and font size 10, but when user starts typing into the textfield, its color should be black and font size 14. I have tried this:
textField.attributedPlaceholder = NSAttributedString(string: placeholderText,
attributes: [NSAttributedString.Key.foregroundColor: Color.iPhoneGrayColor, NSAttributedString.Key.font: UIFont(name: "SourceSansPro-Regular", size: 10)!])
textField.textColor = UIColor.black
textField.font = UIFont(name: "SourceSansPro-Regular", size: 14)!
But, my placeholder font size is getting overridden by textfield.font, so I am unable to get placeholder of size 10. Where am I going wrong? Tried this for couple of hours now. Any help will be appreciated.
Simply set the placeholder after setting the font since setting the font also applies to the placeholder (see https://developer.apple.com/documentation/uikit/uitextfield/1619604-font):
textField.font = ...
textField.textColor = ...
textField.attributedPlaceholder = ...
Try this code into your viewDidLoad() method:
textField.attributedPlaceholder = NSAttributedString(string: placeholderText, attributes: [NSAttributedString.Key.foregroundColor: Color.iPhoneGrayColor, NSAttributedString.Key.font: UIFont(name: "SourceSansPro-Regular", size: 10)!])
textField.textColor = UIColor.black
textField.font = UIFont(name: "SourceSansPro-Regular", size: 14)!
Just try this code, take the UITextFieldEditingChanged action outlet and use as below.
#IBAction func nameTextFieldEditingChanged(_ sender: UITextField) {
if sender.text?.isEmpty ?? true {
//placeholder text size set here
textField.font = UIFont(name: "SourceSansPro-Regular", size: 10)!
} else {
// When user starting typing
textField.textColor = UIColor.black
textField.font = UIFont(name: "SourceSansPro-Regular", size: 14)!
}
}
If any doubt please comment ].
Just try this code may help you for further:
var myMutableStringTitle = NSMutableAttributedString()
let Name = "Enter Title" // PlaceHolderText
myMutableStringTitle = NSMutableAttributedString(string:Name, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 20.0)!]) // Font
myMutableStringTitle.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range:NSRange(location:0,length:Name.characters.count)) // Color
txtTitle.attributedPlaceholder = myMutableStringTitle

Change Navigation Bar Attributes in Only One ViewController

I have an application's navigation bar set in the AppDelegate.
In one of my ViewController's I'm trying to set the attributes differently - they work however when I get to other VC's the still persist.
What is the correct way to implement this?
AppDelegate.swift
UINavigationBar.appearance().titleTextAttributes = [
NSFontAttributeName: UIFont(name: "MuseoSans-500", size: 19)!,
NSForegroundColorAttributeName: UIColor.white
]
UINavigationBar.appearance().tintColor = UIColor.white
SharkViewController.swift
// MARK: - View Did Load
override func viewDidLoad() {
super.viewDidLoad()
UINavigationBar.appearance().titleTextAttributes = [
NSFontAttributeName: UIFont(name: "MuseoSans-500", size: 19)!,
NSForegroundColorAttributeName: UIColor.black
]
}
Do it in your viewWillAppear:
UINavigationBar.appearance().titleTextAttributes = [
NSFontAttributeName: UIFont(name: "MuseoSans-500", size: 19)!,
NSForegroundColorAttributeName: UIColor.black
]
And then when you leave the view, in your viewWillDisappear, you just reset the orgiginal value.
The best way is to change the appearance of the navigationItem of your viewController:
let appearance = UINavigationBarAppearance()
appearance.titleTextAttributes = [.font: UIFont(name: "MuseoSans-500", size: 19)!]
navigationItem.standardAppearance = appearance
This will change only your viewController appearance and you can do it either in the viewDidLoad or viewWillAppear.
You also can use on viewDidLoad: or viewWillAppear the following code:
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "MuseoSans-500", size: 19)!,
NSForegroundColorAttributeName: UIColor.black]

Custom font on UINavigationBar not working

I have no idea why this is not working, I've tried it with many valid strings (cFont = "Courier" by the way but I've tried "Chalkduster" and a few others to test), the colors right above are working but the font is not. Any ideas?
Function is right below the didFinishLaunchingWithOptions in AppDelegate.
func setUpAppearance() {
//Work fine
window?.tintColor = Colors().navBackgroundColor
UINavigationBar.appearance().barTintColor = Colors().navBackgroundColor
UINavigationBar.appearance().tintColor = Colors().navTextColor
//Doesn't work
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: cFont, size: 24)!]
}
Thank you for the help
Got it! Didn't realize I had to add a separate instance for the bar button items as well as NSForeGroundColorAttributeName.
Updated function works fine...
func setUpAppearance() {
window?.tintColor = Colors().navBackgroundColor
UINavigationBar.appearance().barTintColor = Colors().navBackgroundColor
UINavigationBar.appearance().tintColor = Colors().navTextColor
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: cFont, size: 24)!, NSForegroundColorAttributeName: Colors().navTextColor]
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: cFont, size: 24)!, NSForegroundColorAttributeName: Colors().navTextColor], forState: .Normal)
}

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)

Could not find an overload for “init” that accepts the supplied arguments SWIFT

I used this code
self.navigationController?.navigationBar.titleTextAttributes =
[NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 20),
NSForegroundColorAttributeName: UIColor.whiteColor()]
and I'm getting error "Could not find an overload for “init” that accepts the supplied arguments"
UIFont(name:size:) is now a failable initializer -- it will return nil if it can't find that font and crash your app if you unwrap the return value. Use this code to safely get the font and use it:
if let font = UIFont(name: "HelveticaNeue-Light", size: 20) {
self.navigationController?.navigationBar.titleTextAttributes =
[NSFontAttributeName: font,
NSForegroundColorAttributeName: UIColor.whiteColor()]
}
Use this
self.navigationController?.navigationBar.titleTextAttributes =
[NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 20)!,
NSForegroundColorAttributeName: UIColor.whiteColor()!]
or this one
if let font = UIFont(name:"HelveticaNeue-Light", size: 20.0) {
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: font]
}
Another approach is to build up a dictionary before setting titleTextAttributes. This just avoids you the else(s), which would be more beneficial in cases where you wanted to set further parameters also using failable initialisers. Eg:
var attributes : [NSObject : AnyObject] = [NSForegroundColorAttributeName : UIColor.whiteColor()]
if let font = UIFont(name: "Helvetica", size: 20) {
attributes[NSFontAttributeName] = font
}
if let someData = NSData(contentsOfFile: "dataPath") {
attributes["imageData"] = someData
}
self.myObject.attributes = attributes

Resources