Learn Courses My Dashboard

How to change the font size of a button in Xcode in an action?


I am in Xcode in the interface builder storyboard. In an action I wanted when the user presses a button the buttons title becomes an X at a certain font size. I have tried for a long long time and still couldn’t find anything. I will be forever grateful if you could help me :grin:.

 @IBAction func testX(_ sender: UIButton) {
sender.titleLabel?.font = [UIFont withSize(75.0)];

I tried this it doesn’t work.

Looks like google says to,

myButton.titleLabel?.font = UIFont(name: "GillSans-Italic", size: 20)

So try deleting the name: portion if you don’t want a specific name. I don’t personally know UI code at all though.

Thank you anyways :grin:. It’s not so simple to do that in UIKit.


Welcome to the community.

Here is a method I found to achieve a change in Font size when you tap on the Button.

Create an @IBOutlet for your button that you have added in storyboard. eg

@IBOutlet weak var myButton: UIButton!

In your @IBAction that you have for your button, do this:

    @IBAction func testX(_ sender: Any) {
        let customButtonTitle = NSMutableAttributedString(string: "MyButton", attributes: [
            NSAttributedString.Key.font: UIFont.systemFont(ofSize: 75.0),
        myButton.setAttributedTitle(customButtonTitle, for: .normal)

When you tap on the button the font should change to 75 points.