【问题标题】:Setting the titleLabel.font property of a of a UIButton not working设置 UIButton 的 titleLabel.font 属性不起作用
【发布时间】:2013-10-20 19:42:23
【问题描述】:

在当前使用 iOS 7 时,我正在尝试增加 UIButton 的 titleLabel 的字体大小。我就是这样做的,

[[_centerButton titleLabel] setFont:[UIFont boldSystemFontOfSize:28.0]];

但是这并没有做任何事情。每次我用不同的大小编译时,字体总是保持不变。即使我完全删除该行,字体也保持不变。显然它坚持默认大小。

如何增加 UIButton 标题的字体大小?

【问题讨论】:

    标签: objective-c uibutton ios7 uifont


    【解决方案1】:

    使用Xcode 13,我必须在Interface Builder 上将Style 属性从Plain 更改为Default

    然后在设置标题后,我终于可以毫无问题地以编程方式更改字体:

    button.setTitle("The title", for: .normal)
    button.titleLabel?.font = UIFont(name: "Montserrat-SemiBold", size: 15)
    

    【讨论】:

    • 谢谢!这是我的情况,对于那些拥有设计系统或类似系统并且不想自定义故事板的人,您可以button.configuration = nil。有关此 iOS 15 更改的更多信息,请查看here
    • 是的,这真的救了我。谢谢你。不知道为什么 Apple 觉得有必要在这里修修补补。
    • 那么风格到底是如何运作的呢?如果我们将样式设置为普通。使用的字体是什么?和它的重量!谁能解释一下?
    • 这对我有用,但我不确定发生了什么。
    【解决方案2】:

    查看https://developer.apple.com/library/ios/documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/occ/instp/UIButton/titleLabel上的官方 UIButton 类参考

    它说你实际上可以用你的方法设置字体:

    button.titleLabel.font = [UIFont systemFontOfSize: 12];
    

    【讨论】:

    • 作者似乎意识到该方法是正确的,他们显然想知道为什么它在他们的情况下不起作用。如果您没有足够的信息来回答问题,您可以提出澄清问题。说“它应该有效”而不是答案。
    【解决方案3】:

    我也遇到了同样的问题,你用过界面生成器吗?在我将 title 属性设置为 Plain 之后,它就可以工作了,非常非常奇怪。

    【讨论】:

      【解决方案4】:

      您在使用情节提要吗?如果是这样,当您单击 UIButton 时,您可以在属性检查器中看到一个名为 Font 的属性。默认情况下,它显示为“自定义”(显示为空白)。单击 T 符号并选择系统。然后你可以选择字体大小。

      希望对你有帮助。

      【讨论】:

        【解决方案5】:

        有点晚了...titleLabel 的一些属性在 setTitle() 之后被重置。link

        【讨论】:

          【解决方案6】:

          如果您尝试使用 iOS 15 中的新 UIButton.configuration 执行此操作,则更改 titlelabel?.font 将不起作用,因为 configuration 字段将使用系统样式覆盖它,因为您现在需要设置 @ 987654324@.

          您需要创建一个AttributedString 并设置configuration?.attributedTitle = yourAttributedString

          我的整个应用程序都是程序化的,所以这里是一个我创建的自定义按钮“红色药丸”样式按钮的示例,它使用方便的 init 动态设置按钮标题。

          class APButton: UIButton {
          
              override init(frame: CGRect) {
                  super.init(frame: frame)
                  configure()
              }
              
              required init?(coder: NSCoder) {
                  fatalError("init(coder:) has not been implemented")
              }
              
              convenience init(title: String) {
                  self.init(frame: .zero)
                  setDynamicTitle(title: title)
              }
              
              private func configure() {
                  configuration = .filled()
                  configuration?.cornerStyle = .capsule
                  configuration?.baseBackgroundColor = .red
                  configuration?.baseForegroundColor = .white
          
                  translatesAutoresizingMaskIntoConstraints = false
              }
              
              private func setDynamicTitle(title: String) {
                  let font = UIFont.systemFont(ofSize: 20)
                  let container = AttributeContainer([NSAttributedString.Key.font: font])
                  let attribString = AttributedString(title, attributes: container)
                  
                  configuration?.attributedTitle = attribString
              }
          
          }
          

          【讨论】:

            【解决方案7】:

            在您的情况下,问题在于您没有设置 UIControlStateNormal 应该得到特定的 fontSize

            我通常创建一个UIButton 类型的类,并在awakeFromNib 中设置fontSizeLook at this post.

            【讨论】:

              猜你喜欢
              • 2021-12-14
              • 1970-01-01
              • 2012-10-16
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-09-10
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多