We must add all the extensions of the associated UIFramework, for which we must use the dynamic font size of the custom font in iOS-11 +, for example UILabel, UIButton, UITextField, UITextView. The code is given in quick 4.2. We just need to use these custom classes instead of the native ios classes to see the effect of dynamic fonts in the application.
Here's the Shortcut first:
class AppLabel: UILabel { override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { super.traitCollectionDidChange(previousTraitCollection) if #available(iOS 11.0, *) { self.font = UIFontMetrics.default.scaledFont(for: self.font) self.adjustsFontForContentSizeCategory = true // Fallback on earlier versions } else { // Fallback on earlier versions } } }
Here is the 2nd UIButton:
class AppButton: UIButton { override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { super.traitCollectionDidChange(previousTraitCollection) if #available(iOS 11.0, *) { self.titleLabel?.font = UIFontMetrics.default.scaledFont(for: self.titleLabel?.font ?? UIFont()) self.titleLabel?.adjustsFontForContentSizeCategory = true } else {
Here's the third UITextField:
class AppTextField: UITextField { override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { super.traitCollectionDidChange(previousTraitCollection) if #available(iOS 11.0, *) { self.font = UIFontMetrics.default.scaledFont(for: self.font ?? UIFont()) self.adjustsFontForContentSizeCategory = true } else {
Last in UITextView:
class AppTextView: UITextView { override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { super.traitCollectionDidChange(previousTraitCollection) if #available(iOS 11.0, *) { self.font = UIFontMetrics.default.scaledFont(for: self.font ?? UIFont()) self.adjustsFontForContentSizeCategory = true } else {
Umair Ali Nov 30 '18 at 13:57 2018-11-30 13:57
source share