I am trying to change the font of a UIButton using Swift...
myButton.font = UIFont(name: \"...\", 10)
However .font
is depreca
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)
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.
Take a look here.
You should set the font of the button's titleLabel instead.
myButton.titleLabel!.font = UIFont(name: "...", 10)
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)