Equivalent of dynamic type “automatically adjusts

2020-06-09 03:37发布

问题:

A UILabel has a Dynamic Type: Automatically Adjusts Font check-box in the Attributes Inspector in Interface Builder.

Is there an equivalent in Interface Builder for automatically adjusting the font size of a UIButton, or does this have to be handled in code?

I'm using Xcode 9.0 beta 6, targeting iOS 11.

回答1:

Apparently there isn't, but it's not very hard to fix. You can make an extension on UIButton with an @IBInspectable property:

extension UIButton {

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

Now you can simply turn it on for every UIButton (or any of its subclasses).