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

匿名 (未验证) 提交于 2019-12-03 08:41:19

问题:

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 Type size in iOS (Apple has been encouraging this since iOS 7?) I also need to use the built-in font Chalkboard SE, which is not used by default for the "text styles" fonts. I am currently using a custom font as shown in the image, but need the font to change size in accordance with the user's Dynamic Type/Accessibility Sizes preference just as all the Text Styles fonts do. The best Text Styles option is Title 1, but the font/typeface is unacceptable.

回答1:

Although you can't specify both a custom font and a preferred text style via Storyboard, it's not difficult to programmatically specify a dynamic type size for your custom font:

When you receive a UIContentSizeCategoryDidChangeNotification, use the same code to update your label's font.



回答2:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!