swift UIButton字体大小不变

mspsb9vt  于 2023-04-04  发布在  Swift
关注(0)|答案(4)|浏览(263)
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

3zwjbxry

3zwjbxry1#

在Xcode 13中,UIButton有四种类型,分别是Plain,Grain,Tinted,Filled。当你在故事板中创建一个按钮时,按钮类型会自动设置为Plain,这意味着新的UIButton配置是打开的。如果你想要旧的行为,你必须将样式plain设置为default
或者,如果你想要上面的样式之一。你需要设置字体像

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

eeq64g8w2#

只需在故事板中将按钮样式从普通更改为默认

6tdlim6h

6tdlim6h3#

注意,titleTextAttributesTransformerUIButton.Configuration的参数,而不是UIButton,因此可能的实现是:

var config = UIButton.Configuration.plain()
config.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
   var outgoing = incoming
   outgoing.font = UIFont(name: "AlmoniTzarAAA", size: 20) ?? .systemFont(ofSize: 20)
   return outgoing
}
llycmphe

llycmphe4#

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

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

我会希望帮助你;)

相关问题