How to change the font size of a button in Xcode

Hi

I am in Xcode in the interface builder storyboard trying to make a tic-tac-toe game. In an action I wanted when the user presses a button an X appears at a certain font size. I have looked for a long time around the internet and still couldn't find anything. I will be forever grateful if you could help me 😁.

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

I tried this it doesn't work.

Post not yet marked as solved Up vote post of BarryIlowitz Down vote post of BarryIlowitz
1.3k views

Replies

Problem is because you have it in the IBAction…

Have a look here on how to achieve it with configuration:

https://stackoverflow.com/questions/70977386/how-to-set-different-font-size-for-uibutton-when-it-is-pressed

You can also simply work around the issue with a dispatch:

     @IBAction func testX(_ sender: UIButton) {
          DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1)  { 
               sender.titleLabel?.font = sender.titleLabel?.font.withSize(75)  // really HUGE
          }
    }
  • May need to increase slightly the delay. 0.2 or 0.3 to be safe.

  • Thank you so much. This is huge relief I was searching for hours! I am still testing it out. Very appreciated!

  • Part 1: So I ended up testing your solution. Let me tell 😁 you, you get an A+ for effort. It works. However if I initially had the button set at a 75 font size and then when it got pressed I have this code

    @IBAction func testX(_ sender: UIButton) { DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) { sender.titleLabel?.font = sender.titleLabel?.font.withSize(35) // really HUGE } }

Thank you so much. This is huge relief I was searching for hours! I am still testing it out. Very appreciated!