Skip to content

Instantly share code, notes, and snippets.

@popcornomnom
Last active June 13, 2020 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save popcornomnom/90fa4303784876bee7940fd69051a93a to your computer and use it in GitHub Desktop.
Save popcornomnom/90fa4303784876bee7940fd69051a93a to your computer and use it in GitHub Desktop.
///optional type in case if you want turn off acessability support
private let acessabilitySettings: DynamicTypeSettings? = DynamicTypeSettings()
private class DynamicTypeSettings {
let maxPointSizeScaleFactor: CGFloat
init(maxPointSizeScaleFactor: CGFloat = 1.25) {
self.maxPointSizeScaleFactor = maxPointSizeScaleFactor
UILabel.appearance().adjustsFontForContentSizeCategory = true
UITextField.appearance().adjustsFontForContentSizeCategory = true
}
}
// [Family, Size, CustomWeight enums](https://gist.github.com/popcornomnom/5288be0c003fedff3e9d43c332837dca)
convenience init(_ family: Family = .defaultFamily,
_ size: Size, _ weight: CustomWeight) {
if let acessabilitySettings = acessabilitySettings {
let originalFont = UIFont(name: UIFont.stringName(family, weight), size: size.rawValue)!
let scaledFont = UIFontMetrics.default.scaledFont(for: originalFont,
maximumPointSize: originalFont.pointSize *
acessabilitySettings.maxPointSizeScaleFactor)
self.init(descriptor: scaledFont.fontDescriptor, size: scaledFont.pointSize)
} else {
self.init(name: UIFont.stringName(family, weight), size: _size)!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment