Skip to content

Instantly share code, notes, and snippets.

@marinofelipe
Last active March 13, 2019 04:13
Show Gist options
  • Save marinofelipe/566cf6d7b379a02a1463561e935862bf to your computer and use it in GitHub Desktop.
Save marinofelipe/566cf6d7b379a02a1463561e935862bf to your computer and use it in GitHub Desktop.
Blog post / Dynamic Type - Defines scalable fonts behaviour and extends itself when conditionally conforms to raw representable of raw type string (what we use in our custom fonts)
protocol ScalableFont {
func scaled(forTextStyle textStyle: UIFont.TextStyle?, maxSize: CGFloat?) -> UIFont
}
extension UIFont: ScalableFont {
func scaled(forTextStyle textStyle: UIFont.TextStyle? = nil, maxSize: CGFloat? = nil) -> UIFont {
let fontMetrics = textStyle != nil ? UIFontMetrics(forTextStyle: textStyle!) : UIFontMetrics.default
if let maxSize = maxSize {
return fontMetrics.scaledFont(for: self, maximumPointSize: maxSize)
}
return fontMetrics.scaledFont(for: self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment