How to honor Dynamic Type Accessibility Sizes with a custom font in an iOS storyboard

后端 未结 3 735
我在风中等你
我在风中等你 2021-02-19 07:27

How can I use the dynamic type text style \"Title 1\" and set the font face to the built-in font Chalkboard SE for a UILabel in a storyboard?

I need to honor the Dynamic

3条回答
  •  死守一世寂寞
    2021-02-19 07:37

    Try my solution without need to listening for NSNotification.Name.UIContentSizeCategoryDidChange.

    Simply override traitCollectionDidChange on any UIView or UITableViewCell and update/calulate the fontsize with the similar TextStyle.

    You can simply test it with any simulated Device and the Accessibility Inspector on MacOS.

     override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
           super.traitCollectionDidChange(previousTraitCollection)
    
            let pointSize = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .title1).pointSize
            titleLabel.font = titleLabel.font.withSize(pointSize)
        }
    

提交回复
热议问题