UIButton 字体大小没有改变

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

谁能告诉我这段代码有什么问题? IB 中的标题为空。我成功设置了标题。但字体大小没有改变。

https://i.stack.imgur.com/lkLjp.png

yourButton.titleLabel?.font =  UIFont(name: YourfontName, size: 20)

yourButton.titleLabel?.font = .systemFont(ofSize: 12)

希望能帮到你;)

在Xcode13中,UIButton有四种类型Plain,Grain,Tinted,Filled。当你在storyboard中创建一个按钮时,按钮类型自动设置为Plain,这意味着新的UIButton配置已经开启。如果你想要旧的行为,你必须将样式 plain 设置为 default.

或者,如果你想要上面的样式之一。您需要将字体设置为

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