Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 205
Analog Avatar asked Sep 10 '17 09:09

Analog


People also ask

What is dynamic Type Swift?

The Dynamic Type feature allows users to choose the size of textual content displayed on the screen. It helps users who need larger text for better readability. It also accommodates those who can read a smaller text, allowing more information to appear on the screen.

What is dynamic typing in iOS?

Dynamic Type is a feature that was introduced in iOS 7 allowing users to change the default font size used across iOS. It is intended predominantly to support visually impaired users but in practice there are many iOS users who simply prefer a smaller / larger reading size for a variety of reasons.

How do I change font size on labels in Swift?

Change Font And Size Of UILabel In Storyboard XIB file, open it in the interface builder. Select the label and then open up the Attribute Inspector (CMD + Option + 5). Select the button on the font box and then you can change your text size or font.


2 Answers

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).

Interface Builder

like image 195
bompf Avatar answered Oct 30 '22 22:10

bompf


Well there seems to be a way to do it in the Interface Builder only. I use Xcode version 11.5.

1. First set as usual a dynamic font type:

Dynamic Font for UIButton

I choose Subhead because according to this link it will be 15 pt height which was the font size I had previously set for the button.

2. Instead of setting the titleLabel.adjustsFontForContentSizeCategory in code, you can set it in the "User Defined Runtime Attributes" section in Xcode.

User Defined Runtime Attributes for UIButton

like image 28
Bruno Bieri Avatar answered Oct 31 '22 00:10

Bruno Bieri