Last active
May 17, 2020 08:20
-
-
Save popcornomnom/080f836745d462a540da649a25983793 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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