Skip to content

Instantly share code, notes, and snippets.

@feighter09
Last active June 25, 2021 19:24
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save feighter09/721c270897efb9b7381d to your computer and use it in GitHub Desktop.
Save feighter09/721c270897efb9b7381d to your computer and use it in GitHub Desktop.
Set global font for iOS app in one place
// 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)
}
}
// 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)
}
}
@moderateepheezy
Copy link

How do i use this?

@aju10
Copy link

aju10 commented Mar 1, 2018

Usage?

@PodalakurPrasanth
Copy link

how to use this class ?

@vinod1988
Copy link

Here is the use case.

On AppDelegate didFinishLaunchingWithOptions

Call it like this UIFont. initialize()

That's it

@HARIKIRAN7382
Copy link

How to override the entire font like semi bold and Heavy (System Fonts)?

This Seems to override only Regular, Italic and Bold. Not Workout For Remaining Fonts Hot To Fix it.

Can We override Entire?

@zanyfly
Copy link

zanyfly commented Apr 17, 2020

how can i prevent changing font only uitextfiled

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment