Search code examples
swiftuikit

UIButton font size isn't changing


private func updateViewFromModel() {
    for index in cardButtons.indices {
        let button = cardButtons[index]
        let card = game.cards[index]
        if card.isFaceUp {
            button.setTitle(emoji(for: card), for: .normal)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 50)
            button.backgroundColor = .lightGray
        } else {
            button.setTitle("", for: .normal)
            button.backgroundColor = card.isMatched ? .clear : .systemIndigo
        }
        
    }
}

Can anyone tell me what's wrong with this code? title in IB is empty. I successfully set the title. but font size isn't changing.


Solution

  • In Xcode 13 ,UIButton has four type are Plain,Grain,Tinted,Filled .When you create a button in storyboard , button type automatically set with Plain that means new UIButton configurations is on. If you want to old behaviour , you must set style plain to default.

    Or , If you want one of the style above . You need to set font like

    button.configuration?.titleTextAttributesTransformer =
       UIConfigurationTextAttributesTransformer { incoming in
         var outgoing = incoming
         outgoing.font = UIFont.systemFont(ofSize: 50)
         return outgoing
     }