I tried two ways:
Method 1:
label.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.body)
label.adjustsFontForContentSizeCategory = true
This works fine, even when the preferred text size is changed in the Settings, the text size changes automatically, even before when I go back to the app. But it only works with the system font (San Francisco).
Method 2:
To use a custom font, I add an extension to UIFontDescriptor
:
//from this answer http://stackoverflow.com/a/35467158/2907715
extension UIFontDescriptor {
private struct SubStruct {
static var preferredFontName: String = "Avenir-medium"
}
static let fontSizeTable : NSDictionary = [
UIFontTextStyle.headline: [
UIContentSizeCategory.accessibilityExtraExtraExtraLarge: 23,
UIContentSizeCategory.accessibilityExtraExtraLarge: 23,
UIContentSizeCategory.accessibilityExtraLarge: 23,
UIContentSizeCategory.accessibilityLarge: 23,
UIContentSizeCategory.accessibilityMedium: 23,
UIContentSizeCategory.extraExtraExtraLarge: 23,
UIContentSizeCategory.extraExtraLarge: 21,
UIContentSizeCategory.extraLarge: 19,
UIContentSizeCategory.large: 17,
UIContentSizeCategory.medium: 16,
UIContentSizeCategory.small: 15,
UIContentSizeCategory.extraSmall: 14
],
UIFontTextStyle.subheadline: [
UIContentSizeCategory.accessibilityExtraExtraExtraLarge: 21,
UIContentSizeCategory.accessibilityExtraExtraLarge: 21,
UIContentSizeCategory.accessibilityExtraLarge: 21,
UIContentSizeCategory.accessibilityLarge: 21,
UIContentSizeCategory.accessibilityMedium: 21,
UIContentSizeCategory.extraExtraExtraLarge: 21,
UIContentSizeCategory.extraExtraLarge: 19,
UIContentSizeCategory.extraLarge: 17,
UIContentSizeCategory.large: 15,
UIContentSizeCategory.medium: 14,
UIContentSizeCategory.small: 13,
UIContentSizeCategory.extraSmall: 12
],
UIFontTextStyle.body: [
UIContentSizeCategory.accessibilityExtraExtraExtraLarge: 53,
UIContentSizeCategory.accessibilityExtraExtraLarge: 47,
UIContentSizeCategory.accessibilityExtraLarge: 40,
UIContentSizeCategory.accessibilityLarge: 33,
UIContentSizeCategory.accessibilityMedium: 28,
UIContentSizeCategory.extraExtraExtraLarge: 23,
UIContentSizeCategory.extraExtraLarge: 21,
UIContentSizeCategory.extraLarge: 19,
UIContentSizeCategory.large: 17,
UIContentSizeCategory.medium: 16,
UIContentSizeCategory.small: 15,
UIContentSizeCategory.extraSmall: 14
],
UIFontTextStyle.caption1: [
UIContentSizeCategory.accessibilityExtraExtraExtraLarge: 18,
UIContentSizeCategory.accessibilityExtraExtraLarge: 18,
UIContentSizeCategory.accessibilityExtraLarge: 18,
UIContentSizeCategory.accessibilityLarge: 18,
UIContentSizeCategory.accessibilityMedium: 18,
UIContentSizeCategory.extraExtraExtraLarge: 18,
UIContentSizeCategory.extraExtraLarge: 16,
UIContentSizeCategory.extraLarge: 14,
UIContentSizeCategory.large: 12,
UIContentSizeCategory.medium: 11,
UIContentSizeCategory.small: 11,
UIContentSizeCategory.extraSmall: 11
],
UIFontTextStyle.caption2: [
UIContentSizeCategory.accessibilityExtraExtraExtraLarge: 17,
UIContentSizeCategory.accessibilityExtraExtraLarge: 17,
UIContentSizeCategory.accessibilityExtraLarge: 17,
UIContentSizeCategory.accessibilityLarge: 17,
UIContentSizeCategory.accessibilityMedium: 17,
UIContentSizeCategory.extraExtraExtraLarge: 17,
UIContentSizeCategory.extraExtraLarge: 15,
UIContentSizeCategory.extraLarge: 13,
UIContentSizeCategory.large: 11,
UIContentSizeCategory.medium: 11,
UIContentSizeCategory.small: 11,
UIContentSizeCategory.extraSmall: 11
],
UIFontTextStyle.footnote: [
UIContentSizeCategory.accessibilityExtraExtraExtraLarge: 19,
UIContentSizeCategory.accessibilityExtraExtraLarge: 19,
UIContentSizeCategory.accessibilityExtraLarge: 19,
UIContentSizeCategory.accessibilityLarge: 19,
UIContentSizeCategory.accessibilityMedium: 19,
UIContentSizeCategory.extraExtraExtraLarge: 19,
UIContentSizeCategory.extraExtraLarge: 17,
UIContentSizeCategory.extraLarge: 15,
UIContentSizeCategory.large: 13,
UIContentSizeCategory.medium: 12,
UIContentSizeCategory.small: 12,
UIContentSizeCategory.extraSmall: 12
],
]
final class func preferredDescriptor(textStyle: String) -> UIFontDescriptor {
let contentSize = UIApplication.shared.preferredContentSizeCategory
let style = fontSizeTable[textStyle] as! NSDictionary
return UIFontDescriptor(name: SubStruct.preferredFontName, size: CGFloat((style[contentSize] as! NSNumber).floatValue))
}
}
and in viewDidLoad()
:
label.font = UIFont(descriptor: UIFontDescriptor.preferredDescriptor(textStyle: UIFontTextStyle.body.rawValue), size: 0)
NotificationCenter.default.addObserver(self, selector:#selector(self.userChangedTextSize(notification:)), name: NSNotification.Name.UIContentSizeCategoryDidChange, object: nil)
here is the userChangedTextSize
function:
func userChangedTextSize(notification: NSNotification) {
label.font = UIFont(descriptor: UIFontDescriptor.preferredDescriptor(textStyle: UIFontTextStyle.body.rawValue), size: 0)
}
The problem with this method is that the text size won't change until the user goes back to the app, and then the user would see the old text size change to the new size, which is not ideal.
Could I have the best of both worlds: a custom font with a size that changes automatically in the background?