Skip to content

Instantly share code, notes, and snippets.

@danielemaddaluno
Forked from feighter09/Fonts.swift
Last active February 27, 2019 22:03
Show Gist options
  • Select an option

  • Save danielemaddaluno/f59674cabfc6ffb79712078a4cdc29db to your computer and use it in GitHub Desktop.

Select an option

Save danielemaddaluno/f59674cabfc6ffb79712078a4cdc29db to your computer and use it in GitHub Desktop.

Revisions

  1. danielemaddaluno revised this gist Feb 27, 2019. 1 changed file with 84 additions and 56 deletions.
    140 changes: 84 additions & 56 deletions Fonts.swift
    Original file line number Diff line number Diff line change
    @@ -1,61 +1,89 @@
    public protocol SwizzlingInjection: class {
    static func inject()
    }

    class SwizzlingHelper {

    private static let doOnce: Any? = {
    UIFont.inject()
    return nil
    }()

    static func enableInjection() {
    _ = SwizzlingHelper.doOnce
    }
    }

    extension UIApplication {

    override open var next: UIResponder? {
    // Called before applicationDidFinishLaunching
    SwizzlingHelper.enableInjection()
    return super.next
    }

    }


    // MARK: - Swizzling
    extension UIFont {
    class var defaultFontFamily: String { return "Georgia" }

    override public class func initialize()
    {
    if self == UIFont.self {
    swizzleSystemFont()
    }
    }

    private class func swizzleSystemFont()
    {
    let systemPreferredFontMethod = class_getClassMethod(self, "preferredFontForTextStyle:")
    let mySystemPreferredFontMethod = class_getClassMethod(self, "myPreferredFontForTextStyle:")
    method_exchangeImplementations(systemPreferredFontMethod, mySystemPreferredFontMethod)

    let systemFontMethod = class_getClassMethod(self, "systemFontOfSize:")
    let mySystemFontMethod = class_getClassMethod(self, "mySystemFontOfSize:")
    method_exchangeImplementations(systemFontMethod, mySystemFontMethod)

    let boldSystemFontMethod = class_getClassMethod(self, "boldSystemFontOfSize:")
    let myBoldSystemFontMethod = class_getClassMethod(self, "myBoldSystemFontOfSize:")
    method_exchangeImplementations(boldSystemFontMethod, myBoldSystemFontMethod)

    let italicSystemFontMethod = class_getClassMethod(self, "italicSystemFontOfSize:")
    let myItalicSystemFontMethod = class_getClassMethod(self, "myItalicSystemFontOfSize:")
    method_exchangeImplementations(italicSystemFontMethod, myItalicSystemFontMethod)
    }
    extension UIFont: SwizzlingInjection {
    class var defaultFontFamily: String { return "Satellite" }

    public static func inject() {
    // make sure this isn't a subclass
    guard self === UIFont.self else { return }

    // Do your own method_exchangeImplementations(originalMethod, swizzledMethod) here
    swizzleSystemFont()
    }

    private class func swizzleSystemFont()
    {
    let systemPreferredFontMethod = class_getClassMethod(self, #selector(UIFont.preferredFont(forTextStyle:)))
    let mySystemPreferredFontMethod = class_getClassMethod(self, #selector(UIFont.myPreferredFontForTextStyle(style:)))
    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 {
    private class func myPreferredFontForTextStyle(style: String) -> UIFont
    {
    let defaultFont = myPreferredFontForTextStyle(style) // will not cause stack overflow - this is now the old, default UIFont.preferredFontForTextStyle
    let newDescriptor = defaultFont.fontDescriptor().fontDescriptorWithFamily(defaultFontFamily)
    return UIFont(descriptor: newDescriptor, size: defaultFont.pointSize)
    }

    private class func mySystemFontOfSize(fontSize: CGFloat) -> UIFont
    {
    return myDefaultFontOfSize(fontSize)
    }

    private class func myBoldSystemFontOfSize(fontSize: CGFloat) -> UIFont
    {
    return myDefaultFontOfSize(fontSize, withTraits: .TraitBold)
    }

    private class func myItalicSystemFontOfSize(fontSize: CGFloat) -> UIFont
    {
    return myDefaultFontOfSize(fontSize, withTraits: .TraitItalic)
    }

    private class func myDefaultFontOfSize(fontSize: CGFloat, withTraits traits: UIFontDescriptorSymbolicTraits = []) -> UIFont
    {
    let descriptor = UIFontDescriptor(name: defaultFontFamily, size: fontSize).fontDescriptorWithSymbolicTraits(traits)
    return UIFont(descriptor: descriptor, size: fontSize)
    }
    }
    @objc private class func myPreferredFontForTextStyle(style: String) -> UIFont
    {
    let defaultFont = myPreferredFontForTextStyle(style: 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 private class func mySystemFont(ofSize: CGFloat) -> UIFont
    {
    return myDefaultFont(ofSize: ofSize)
    }
    @objc private class func myBoldSystemFont(ofSize: CGFloat) -> UIFont
    {
    return myDefaultFont(ofSize: ofSize, withTraits: .traitBold)
    }
    @objc private class func myItalicSystemFont(ofSize: CGFloat) -> UIFont
    {
    return myDefaultFont(ofSize: ofSize, withTraits: .traitItalic)
    }
    private class func myDefaultFont(ofSize: CGFloat, withTraits traits: UIFontDescriptorSymbolicTraits = []) -> UIFont
    {
    let descriptor = UIFontDescriptor(name: defaultFontFamily, size: ofSize).withSymbolicTraits(traits)
    return UIFont(descriptor: descriptor!, size: ofSize)
    }
    }
  2. @feighter09 feighter09 revised this gist Aug 16, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions Fonts.swift
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // MARK: - Swizzling
    extension UIFont {
    class var defaultFontFamily: String { return "Georgia" }

    @@ -28,6 +29,7 @@ extension UIFont {
    }
    }

    // MARK: - New Font Methods
    extension UIFont {
    private class func myPreferredFontForTextStyle(style: String) -> UIFont
    {
  3. @feighter09 feighter09 created this gist Aug 16, 2015.
    59 changes: 59 additions & 0 deletions Fonts.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    extension UIFont {
    class var defaultFontFamily: String { return "Georgia" }

    override public class func initialize()
    {
    if self == UIFont.self {
    swizzleSystemFont()
    }
    }

    private class func swizzleSystemFont()
    {
    let systemPreferredFontMethod = class_getClassMethod(self, "preferredFontForTextStyle:")
    let mySystemPreferredFontMethod = class_getClassMethod(self, "myPreferredFontForTextStyle:")
    method_exchangeImplementations(systemPreferredFontMethod, mySystemPreferredFontMethod)

    let systemFontMethod = class_getClassMethod(self, "systemFontOfSize:")
    let mySystemFontMethod = class_getClassMethod(self, "mySystemFontOfSize:")
    method_exchangeImplementations(systemFontMethod, mySystemFontMethod)

    let boldSystemFontMethod = class_getClassMethod(self, "boldSystemFontOfSize:")
    let myBoldSystemFontMethod = class_getClassMethod(self, "myBoldSystemFontOfSize:")
    method_exchangeImplementations(boldSystemFontMethod, myBoldSystemFontMethod)

    let italicSystemFontMethod = class_getClassMethod(self, "italicSystemFontOfSize:")
    let myItalicSystemFontMethod = class_getClassMethod(self, "myItalicSystemFontOfSize:")
    method_exchangeImplementations(italicSystemFontMethod, myItalicSystemFontMethod)
    }
    }

    extension UIFont {
    private class func myPreferredFontForTextStyle(style: String) -> UIFont
    {
    let defaultFont = myPreferredFontForTextStyle(style) // will not cause stack overflow - this is now the old, default UIFont.preferredFontForTextStyle
    let newDescriptor = defaultFont.fontDescriptor().fontDescriptorWithFamily(defaultFontFamily)
    return UIFont(descriptor: newDescriptor, size: defaultFont.pointSize)
    }

    private class func mySystemFontOfSize(fontSize: CGFloat) -> UIFont
    {
    return myDefaultFontOfSize(fontSize)
    }

    private class func myBoldSystemFontOfSize(fontSize: CGFloat) -> UIFont
    {
    return myDefaultFontOfSize(fontSize, withTraits: .TraitBold)
    }

    private class func myItalicSystemFontOfSize(fontSize: CGFloat) -> UIFont
    {
    return myDefaultFontOfSize(fontSize, withTraits: .TraitItalic)
    }

    private class func myDefaultFontOfSize(fontSize: CGFloat, withTraits traits: UIFontDescriptorSymbolicTraits = []) -> UIFont
    {
    let descriptor = UIFontDescriptor(name: defaultFontFamily, size: fontSize).fontDescriptorWithSymbolicTraits(traits)
    return UIFont(descriptor: descriptor, size: fontSize)
    }
    }