How to change font of UIButton with Swift - ios

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

Related

Set Specific Font Weight for UILabel in Swift

When people have asked how to set a bold font, most people suggest:
let boldFont = UIFont.boldSystemFont(ofSize: ___)
But take a look at all the font weights that the standard system font offers:
So my question is how do you set a light, semibold, or heavy font weight? The only way that I know how is:
sectionLabel.font = [UIFont fontWithName:#"TrebuchetMS-Bold" size:18];
However, I'm still asking because this isn't strongly typed. Most other class attributes are set by selecting from a fixed set of options and don't require passing a string that I could mistype. I guess I could set my own global enum... But any better ideas?
I couldn't get the UIFontDescriptor to work with the font weight trait but there is another approach.
let font = UIFont.systemFont(ofSize: 20, weight: .light)
Replace .light with whichever value you want from UIFont.Weight which basically matches the dropdown list shown in your question.
You can use this extension. It assigns the weight to the fontDescriptor's weight key and instantiate your font with this new descriptor.
extension UIFont {
func withWeight(_ weight: UIFont.Weight) -> UIFont {
let newDescriptor = fontDescriptor.addingAttributes([.traits: [
UIFontDescriptor.TraitKey.weight: weight]
])
return UIFont(descriptor: newDescriptor, size: pointSize)
}
}
The very old thread, but someone may be interested in how to do it in Swift.
UIFont.Weight defines all of the options:
ultraLight
thin
light
regular
medium
semibold
bold
heavy
black
you can use simply like that, e.g.:
label.font = UIFont.systemFont(ofSize: size, weight: .bold)
or if you want to keep the previous size:
label.font = UIFont.systemFont(ofSize: label.font!.pointSize, weight: .bold)
Even more:
let font = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight(500))
If you want to use system fonts, for Swift 5 a simple extension would look like this:
extension UIFont {
func withWeight(_ weight: UIFont.Weight) -> UIFont {
UIFont.systemFont(ofSize: pointSize, weight: weight)
}
}

Swift Changing font programmatically

Hi I am currently setting up views below:
func setupViews() {
self.numberLabel = UILabel(frame: .zero)
self.addSubview(self.numberLabel)
self.numberLabel.snp.makeConstraints { (make) in
make.edges.equalToSuperview()
}
self.numberLabel.textAlignment = .center
self.numberLabel.textColor = UIColor.white
self.numberLabel.adjustsFontSizeToFitWidth = true
self.numberLabel.minimumScaleFactor = 0.5
}
I would like to change the font of the text inside the label to bold font, however it's difficult to see an easy way to do so, following the syntax principles above.
For Swift 5+ and upto latest version (Swift 5.4) period
Stylizing the Font (SystemFont)
UIFont.systemFont(ofSize: 17, weight: .medium)
where you can set .weight provides various UIFont.Weight properties as given below
.black
.bold
.heavy
.light
.medium
.regular
.semibold
.thin (Looks very cool and I guess Apple also uses this somewhere)
.ultralight
Note that it's only for default SystemFont only.
AppleDocumentation
Changing the Font
UIFont(name: "HelveticaNeue-Thin", size: 16.0)
where name includes the FontName
you can also specify .weight by writing - before weight (If that font supports that weight as Not all fontFamily supports all
types of UIFont.Weight)
This is more preferable method to use for stylising the font if
you're not using default SystemFont
You just set the font property of the label, for example:
numberLabel.font = UIFont.systemFont(ofSize: 10, weight: 200)
I'm amazed there's nothing on SO already on this.
Take a look at the reference docs too: https://developer.apple.com/documentation/uikit/uilabel
By the way, unless you are operating within a closure, or other contexts where the semantics are ambiguous (e.g. in an initialiser) you don't generally need to use self. prefix.
Hope that helps.
If your just using the system font you can also do
UIFont.boldSystemFont(ofSize: 16.0)
Selecting whichever size you need
Here's the documentation on it

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

How to access the iOS system font programmatically

I am trying to change the font size of the title of a navigation bar. I know I can set its attributes using:
var attributes = [ NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont(name: "the font name", size: 18)! ]
...
self.navigationController?.navigationBar.titleTextAttributes = attributes
What I cannot seem to find is the correct 'System' font name.
I was after the default, a.k.a System, font name. I tried printing all the available fonts only to discover it does not belong to a family and does not seem to have an explicit name.
I think you need:
NSFontAttributeName : UIFont.systemFontOfSize(19.0)
Or the bold version:
NSFontAttributeName : UIFont.boldSystemFontOfSize(19.0)
See this guide for more info on user interface guidelines and fonts.
You can access the system font like this, and even set the weight of the font:
Swift 3, Swift 4
UIFont.systemFont(ofSize: 18, weight: UIFontWeightLight)
Swift 2
UIFont.systemFontOfSize(18, weight: UIFontWeightLight)
For the font weight you have the choice between those constants, there available from iOS 8.2:
UIFontWeightUltraLight,
UIFontWeightThin,
UIFontWeightLight,
UIFontWeightRegular,
UIFontWeightMedium,
UIFontWeightSemibold,
UIFontWeightBold,
UIFontWeightHeavy,
UIFontWeightBlack
SWIFT 4+:
shorter version
UIFont.systemFont(ofSize: 14.0, weight: .regular)
(In line with the answer from Philippe for the latest version)
Swift 4
UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.light)
Besides all the answers, it's a better idea to use system font with system styles instead of defining custom sizes and weights. To access them programmatically, for example for the headline, you can use this method:
let font = UIFont.preferredFont(forTextStyle: .headline)
I know it is a valid code at least for Swift 5.
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName : UIFont.systemFontOfSize(6)]
Just use methods of UIFont (swift):
let sysFont: UIFont = UIFont.systemFontOfSize(UIFont.systemFontSize())
Hope it helps!
Try the below code:
self.navigationController!.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name:"Arial", size:14.0)!, NSForegroundColorAttributeName:UIColor.blackColor()]

changing UITableViewCell text font

I was able to change the font of the tableViewCell text using this code
cell.textLabel?.font = UIFont(name: "Snell Roundhand", size: 40)
However i would also like to make this font bold, so far I've tried to do this by using this code
cell.textLabel?.font = UIFont.boldSystemFontOfSize(40)
Although this does change the font to be bold, it also changes the font back to the standard one and not "Snell Roundhand".
Any idea on why this is? and how to resolve this?
Many thanks
Kieran
You're using the regular font name. To use the bold variation of the font, use this code:
cell.textLabel?.font = UIFont(name: "SnellRoundhand-Bold", size: 40)

Resources