Skip to content

Instantly share code, notes, and snippets.

@nikans
Forked from feighter09/Fonts.swift
Created July 27, 2017 12:29
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 nikans/5993c41470174f51e17aba032ae4f046 to your computer and use it in GitHub Desktop.
Save nikans/5993c41470174f51e17aba032ae4f046 to your computer and use it in GitHub Desktop.
Set global font for iOS app in one place
// Kinda updated for Swift 3.1
// MARK: - Swizzling
extension UIFont {
class var defaultFontFamily: String { return "Georgia" }
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)
}
}
// MARK: - New Font Methods
extension UIFont {
@objc fileprivate class func myPreferredFont(forTextStyle style: String) -> UIFont
{
let defaultFont = myPreferredFont(forTextStyle: style) // will not cause stack overflow - this is now the old, default UIFont.preferredFontForTextStyle
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)
}
@objc 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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment