等效于 Interface Builder 中 UIButton 的动态类型 "automatically adjusts font" 设置?

Equivalent of dynamic type "automatically adjusts font" setting for UIButton in Interface Builder?

UILabel 在 Interface Builder 的属性检查器中有一个 Dynamic Type: Automatically Adjusts Font 复选框。

Interface Builder 中是否有自动调整 UIButton 字体大小的等效项,或者这是否必须在代码中处理?

我正在使用 Xcode 9.0 beta 6,目标是 iOS 11.

显然没有,但修复起来并不难。您可以使用 @IBInspectable 属性:

UIButton 上进行扩展
extension UIButton {

    @IBInspectable
    var adjustsFontForContentSizeCategory: Bool {
        set {
            self.titleLabel?.adjustsFontForContentSizeCategory = newValue
        }
        get {
            return self.titleLabel?.adjustsFontForContentSizeCategory ?? false
        }
    }
}

现在您可以简单地为每个 UIButton(或其任何子类)打开它。

好吧,似乎只有在 Interface Builder 中才能做到这一点。我使用 Xcode 版本 11.5.

1.首先照例设置一个动态字体类型:

我选择 Subhead 因为根据这个 link 它将是 15 pt 高度,这是我之前为按钮设置的字体大小。

2. 除了在代码中设置 titleLabel.adjustsFontForContentSizeCategory,您可以在 Xcode 的“用户定义的运行时属性”部分中设置它。

直接在 IB 上有一个简单的解决方案,没有那么多 extension 在按钮上的痛苦等等。

在界面生成器中

  1. Button TypeSystem 设置为 Custom
  2. 照常设置 FontText Style

可选

  1. 您可以在 .leading.trailing 上设置自动布局约束,以便在设置大辅助文本时,按钮不应超出可见区域。