Skip to content

Instantly share code, notes, and snippets.

@popcornomnom
Last active May 17, 2020 08:20
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/080f836745d462a540da649a25983793 to your computer and use it in GitHub Desktop.
Save popcornomnom/080f836745d462a540da649a25983793 to your computer and use it in GitHub Desktop.
// somewhere here is a declaration of the Font Style parts
// https://gist.github.com/popcornomnom/5288be0c003fedff3e9d43c332837dca
// and func stringName(_, _) -> String
// https://gist.github.com/popcornomnom/1238714fea8d9a8efa109782958a5b99
///optional type in case if you don't want to support scaled font yet
private let autoScaleSettings: AutoScaleSettings? = AutoScaleSettings()
private class AutoScaleSettings {
let mostPopularScreenWidth: CGFloat = 375
let maxScreenWidth: CGFloat = 460 //use to prevent huge font scaling on iPad
let minScaleFactor: CGFloat = 0.93 //to protect scalling of very small sizes
}
extension UIFont {
convenience init(_ family: Family = .defaultFamily,
_ size: Size, _ weight: CustomWeight) {
var size = size.rawValue
if let autoScaleSettings = autoScaleSettings {
var ratio = (min(UIScreen.main.bounds.width, autoScaleSettings.maxScreenWidth)
/ autoScaleSettings.mostPopularScreenWidth)
ratio = max(autoScaleSettings.minScaleFactor, ratio)
if ratio > 1 || (ratio < 1 && size.doesSupportMinScalling) {
_size = round(ratio * _size)
}
}
self.init(name: stringName(family, weight), size: size)!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment