Setting navigation bar text font in Xcode 6.1 - ios

I am using a non-default font in my apps navigation bar, and this worked absolutely fine prior to Xcode 6.1. Now I get an error on the line of code where I am defining the font type and colour.
This is my code:
var attributes = [NSForegroundColorAttributeName: UIColor.whiteColor(),NSFontAttributeName: UIFont(name: "Avenir", size: 24)]
self.navigationController?.navigationBar.titleTextAttributes = attributes
What do I need to change to get this to work again?

UIFont(name:size:) returns an optional, which cannot be used as the value.Change it to this:
var attributes = [NSForegroundColorAttributeName: UIColor.whiteColor(),NSFontAttributeName: UIFont(name: "Avenir", size: 24)!]
By doing this you unwrap the UIFont? value to NSFontAttributeName. I believe you have to make sure the font is actually there to avoid crash.

Related

Font is nil when setting NSFontAttributeName

I'm trying to change my font from medium to regular, but I keep getting nil when setting the font type for NSFontAttributeName. I can get the font as medium without a problem, but when I try to set it to regular programmatically, my program crashes because the font is nil.
let attrsUnselected = [NSFontAttributeName: UIFont(name: "Roboto-
Regular", size: 15)!, NSForegroundColorAttributeName : UIColor.gray]
let attrsSelected = [NSFontAttributeName: UIFont(name: "Roboto-Medium",
size: 15)!,NSForegroundColorAttributeName : UIColor.black]
Before my app crashes, I can see that the font is actually set to regular.
I don't know how many different ways I get validate that the font has been imported into the project.
Check the following:
The font is added to the target
Its listed under "Copy Bundle Resources" on your target
And does it show up in the list of available fonts when you print them?

Navigation bar title not bold

When I'm in the storyboard, the navigation bar title is shown as bold in the Storyboard as default but when you run the app in simulator and device, it isn't bold. Annoyingly it isn't possible to change the font size from interface builder.
Below you can see the difference between what's shown on interface builder and the simulator.
Interface builder:
Simulator:
I'm aware that I can change the font using something like this: self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "YOUR-FONT-NAME-WEIGHT", size: 24)!
That would be perfect but I want to keep the font to the default SF font.
Is there a workaround this? Any reason why this happens?
Use this code in your viewDidLoad to change font size
self.navigationController?.navigationBar.titleTextAttributes = [
NSFontAttributeName: UIFont.systemFont(ofSize: 20, weight: UIFontWeightHeavy)
]
For swift 4:
self.navigationController?.navigationBar.titleTextAttributes = [
NSAttributedStringKey.font: UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.heavy)
]
Before in simulator it looks like
And with that code, it looks like
Simply use UIFont.systemFont to get the default SF font.
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont.systemFont(ofSize:24, weight: .bold)]
Swift 4:
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.heavy)]

UINavigationBar cut off font

I'm using custom font in my application. I've used the code below in AppDelegate to change the UINavigationBar appearance.
let navigationBarAppearance = UINavigationBar.appearance()
navigationBarAppearance.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Pacifico", size: 20)!]
However, the font is getting cut off at the end of the string. The text is "Tell", but you can see that the last character is getting cut off. It works fine if I'm using the default font. Is this a problem with the font itself or it's a bug in Xcode?

Swift NSAttributedString custom fonts

I've read around for different solutions but nothing seems to work. This code creates a nil exception:
[NSFontAttributeName: UIFont(name: "Raleway-SemiBold", size: 16)!]
I have the fonts installed properly and they show up correctly in the app (target is set).
I tried to add the application provided fonts in the plist but nothing happened. I can't edit the items in the array: (they are item0 : string : Raleway-SemiBold.tff).
So basically I'm stuck... Sometimes Swift and Apple environments are great for a programmer, other times (most of the time), they are sooo faulty and need so many workarounds to reach the expected results.
Many thanks in advance for any help.
You're getting an exception because UIFont(name: "Raleway-SemiBold", size: 16) returns nil and you're force-unwrapping it with !.
Instead, use conditional unwrapping:
if let font = UIFont(name: "Raleway-SemiBold", size: 16) {
let attributes = [NSFontAttributeName: font]
// do something with attributes
} else {
// The font "Raleway-SemiBold" is not found
}
You can use UIFont's familyNames() and fontNamesForFamilyName(_:) methods to get the exact string required.
Swift 4
if let font = UIFont(name: "Raleway-SemiBold", size: 16) {
let attributes = [NSAttributedStringKey.font: font]
// do something with attributes
} else {
// The font "Raleway-SemiBold" is not found
}
You Just have to write the correct string name of your font.
Don't write the name that is font file name. (like bodoni-mt-bold.ttf its the file name i have downloaded from any site).
To find out the exact font name follow the image below.
Go to your label select it and go to custom font and then see the name of your custom font in its family. if your custom font name is there then copy that name and past it as a string where u wanna use it. (Note you can't copy font name text you have to write else where then past it)
For Swift 3, here's an update that worked for me:
First you'll set up the font and then create a textAttribute with the NSFontAttributeName:
let font = UIFont(name: "Raleway-SemiBold", size: 16)
textAttribute = [NSFontAttributeName: font as Any, NSForegroundColorAttributeName: UIColor.black]
You can then apply textAttribute to your label, textfield etc.

How to change font of UIButton with Swift

I am trying to change the font of a UIButton using Swift...
myButton.font = UIFont(name: "...", 10)
However .font is deprecated and I'm not sure how to change the font otherwise.
Any suggestions?
Use titleLabel instead. The font property is deprecated in iOS 3.0. It also does not work in Objective-C. titleLabel is label used for showing title on UIButton.
myButton.titleLabel?.font = UIFont(name: YourfontName, size: 20)
However, while setting title text you should only use setTitle:forControlState:. Do not use titleLabel to set any text for title directly.
For Swift 3.0:
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
where "boldSystemFont" and "16" can be replaced with your custom font and size.
As mentioned by many here you can set the font with something like:
button.titleLabel?.font = .systemFont(ofSize: 19.0, weight: .bold)
Just make sure your button has default style though for this to be applicable, otherwise the above gets ignored by the system.
Dot-notation is awesome 👌
btn.titleLabel?.font = .systemFont(ofSize: 12)
If you need to change only size (Swift 4.0):
button.titleLabel?.font = button.titleLabel?.font.withSize(12)
You don't need to force unwrap the titleLabel to set it.
myButton.titleLabel?.font = UIFont(name: YourfontName, size: 20)
Since you're not using the titleLabel here, you can just optionally use it and if it's nil it will just be a no-op.
I'll also add as other people are saying, the font property is deprecated, and make sure to use setTitle:forControlState: when setting the title text.
In Swift 5, you can utilize dot notation for a bit quicker syntax:
myButton.titleLabel?.font = .systemFont(ofSize: 14, weight: .medium)
Otherwise, you'll use:
myButton.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
From the documentation:
The font used to display text on the button. (Deprecated in iOS 3.0. Use the font property of the titleLabel instead.)
If you're having font size issues (your font isn't responding to size changes)...
#codester has the right code:
myButton.titleLabel!.font = UIFont(name: YourfontName, size: 20)
However, my font size wasn't changing. It turns out that I asking for a font that didn't exist ("HelveticaNeue-Regular"). It wasn't causing a crash, but seemed to be just ignoring that font statement because of it. Once I changed the font to something that does exist, changes to "size: x" did render.
we can use different types of system fonts like below
myButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
myButton.titleLabel?.font = UIFont.italicSystemFont(ofSize:UIFont.smallSystemFontSize)
myButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: UIFont.buttonFontSize)
and your custom font like below
myButton.titleLabel?.font = UIFont(name: "Helvetica", size:12)
Take a look here.
You should set the font of the button's titleLabel instead.
myButton.titleLabel!.font = UIFont(name: "...", 10)
If you are setting AttributedString to the UIButton then you can do the below thing.
let attributedText = NSAttributedString(string: "Hello", attributes: [NSAttributedStringKey.font: UIFont(name: "Calibri", size: 19)])
okayButton.setAttributedTitle(attributedText, for: .normal)
You should go through the titleLabel property.
button.titleLabel.font
The font property has been deprecated since iOS 3.0.
This works in Swift 3.0:
btn.titleLabel?.font = UIFont(name:"Times New Roman", size: 20)
Example: button.titleLabel?.font = UIFont(name: "HelveticaNeue-Bold", size: 12)
If you want to use defaul font from it's own family, use for example: "HelveticaNeue"
If you want to specify family font, use for example: "HelveticaNeue-Bold"
This way doesn't work now:
btn.titleLabel?.font = UIFont(name: "Helvetica", size:12)
This works:
btn.titleLabel?.font = UIFont.init(name: "Helvetica", size:12)
To do this using storyboard, go to the attributes inspector while your button is selected. In the third field from the top ("Title") select "Attributed". This will bring up the font drop-down list where you can easily change the font.
this work for me, thanks. I want change text size only not change font name.
var fontSizeButtonBig:Int = 30
btnMenu9.titleLabel?.font = .systemFont(ofSize: CGFloat(fontSizeButtonBig))
Swift 5
myButton.titleLabel?.font = UIFont(name: "yourCustomFont", size: CGFloat(yourFontSize))

Resources