-
-
Save switchCTRL/400d6854f166c4cffed6c9f12a637194 to your computer and use it in GitHub Desktop.
Set global font for iOS app in one place
This file contains hidden or 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
| import UIKit | |
| extension UIFont { | |
| @objc class func myPreferredFont(forTextStyle style: String) -> UIFont { | |
| let defaultFont = myPreferredFont(forTextStyle: style) // don´t know but it works... | |
| let newDescriptor = defaultFont.fontDescriptor.withFamily(defaultFontFamily) | |
| return UIFont(descriptor: newDescriptor, size: defaultFont.pointSize) | |
| } | |
| @objc fileprivate class func mySystemFont(ofSize fontSize: CGFloat) -> UIFont { | |
| return myDefaultFont(ofSize: fontSize) | |
| } | |
| @objc fileprivate class func myBoldSystemFont(ofSize fontSize: CGFloat) -> UIFont { | |
| return myDefaultFont(ofSize: fontSize, withTraits: .traitBold) | |
| } | |
| @objc fileprivate class func myItalicSystemFont(ofSize fontSize: CGFloat) -> UIFont { | |
| return myDefaultFont(ofSize: fontSize, withTraits: .traitItalic) | |
| } | |
| fileprivate class func myDefaultFont(ofSize fontSize: CGFloat, withTraits traits: UIFontDescriptorSymbolicTraits = []) -> UIFont { | |
| let descriptor = UIFontDescriptor(name: defaultFontFamily, size: fontSize).withSymbolicTraits(traits) | |
| return UIFont(descriptor: descriptor!, size: fontSize) | |
| } | |
| } | |
| extension UIFont { | |
| class var defaultFontFamily: String { return "<# Your Font goes here... #>" } | |
| override open class func initialize() { | |
| if self == UIFont.self { | |
| swizzleSystemFont() | |
| } | |
| } | |
| private class func swizzleSystemFont() { | |
| let systemPreferredFontMethod = class_getClassMethod(self, #selector(UIFont.preferredFont(forTextStyle:))) | |
| let mySystemPreferredFontMethod = class_getClassMethod(self, #selector(UIFont.myPreferredFont(forTextStyle:))) | |
| method_exchangeImplementations(systemPreferredFontMethod, mySystemPreferredFontMethod) | |
| let systemFontMethod = class_getClassMethod(self, #selector(UIFont.systemFont(ofSize:))) | |
| let mySystemFontMethod = class_getClassMethod(self, #selector(UIFont.mySystemFont(ofSize:))) | |
| method_exchangeImplementations(systemFontMethod, mySystemFontMethod) | |
| let boldSystemFontMethod = class_getClassMethod(self, #selector(UIFont.boldSystemFont(ofSize:))) | |
| let myBoldSystemFontMethod = class_getClassMethod(self, #selector(UIFont.myBoldSystemFont(ofSize:))) | |
| method_exchangeImplementations(boldSystemFontMethod, myBoldSystemFontMethod) | |
| let italicSystemFontMethod = class_getClassMethod(self, #selector(UIFont.italicSystemFont(ofSize:))) | |
| let myItalicSystemFontMethod = class_getClassMethod(self, #selector(UIFont.myItalicSystemFont(ofSize:))) | |
| method_exchangeImplementations(italicSystemFontMethod, myItalicSystemFontMethod) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment