Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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
You can’t perform that action at this time.