Skip to content

Instantly share code, notes, and snippets.

@lalkrishna
Created September 13, 2020 13:18
Show Gist options
  • Save lalkrishna/ce3abd0a8f2b4ffc9621d59f31e0660d to your computer and use it in GitHub Desktop.
Save lalkrishna/ce3abd0a8f2b4ffc9621d59f31e0660d to your computer and use it in GitHub Desktop.
Make use of custom fonts with the help of extensions. Note: iOS not detecting `black` font weight for Montserrat font. Please comment if you know the solution for it.
// MARK: - Usage
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
UIFont.registerAllCustomFonts()
// Or You can add manually,
// https://developer.apple.com/documentation/uikit/text_display_and_fonts/adding_a_custom_font_to_your_app
return true
}
// Using Default family name
let customMontserratFont: UIFont = .font(weight: .bold, size: 20)
label.font = customMontserratFont
subLabel.font = customMontserratFont.withWeight(.semiBold)
hintLabel = customMontserratFont.regular
// Or other family
let americalFont: UIFont = .font(.americalTypewriter, weight: .heavy, size: 20)
label.font = americalFont
hintLabel = americalFont.withWeight(.light, fontSize: 18)
// MARK: - Implementation
extension UIFont {
enum Fonts {
case americalTypewriter, montserrat
var family: String {
switch self {
case .americalTypewriter:
return "American Typewriter"
case .montserrat:
return "Montserrat"
}
}
}
var ultraLight: UIFont { withWeight(.ultraLight) }
var thin: UIFont { withWeight(.thin) }
var light: UIFont { withWeight(.light) }
var regular: UIFont { withWeight(.regular) }
var medium: UIFont { withWeight(.medium) }
var semibold: UIFont { withWeight(.semibold) }
var bold: UIFont { withWeight(.bold) }
var heavy: UIFont { withWeight(.heavy) }
var black: UIFont { withWeight(.black) }
static func registerAllCustomFonts() {
// Note: iOS not detecting `black` font weight for Montserrat font. So I'm forced to use
// UIFont(name: "Montserrat-Black", size: 20)
UIFont.register("Montserrat-Black")
UIFont.register("Montserrat-Bold")
UIFont.register("Montserrat-ExtraBold") // UIFont.Weight.heavy
UIFont.register("Montserrat-SemiBold")
UIFont.register("Montserrat-Light")
UIFont.register("Montserrat-ExtraLight") // UIFont.Weight.ultraLight
UIFont.register("Montserrat-Medium")
UIFont.register("Montserrat-Regular")
UIFont.register("Montserrat-Thin")
}
}
extension UIFont {
static func register(_ name: String, extension format: String = "ttf") {
guard let url = Bundle.main.url(forResource: name, withExtension: format) else { return }
CTFontManagerRegisterFontsForURL(url as CFURL, .process, nil)
}
static func font(_ font: Fonts = .montserrat, weight: UIFont.Weight, size: CGFloat) -> UIFont {
let descriptor = UIFontDescriptor.with(family: font.family, weight: weight)
return UIFont(descriptor: descriptor, size: size)
}
func withWeight(_ weight: UIFont.Weight, fontSize: CGFloat? = nil) -> UIFont {
let descriptor = fontDescriptor.with(family: familyName, weight: weight)
return UIFont(descriptor: descriptor, size: fontSize ?? pointSize)
}
}
extension UIFontDescriptor {
static func with(family: String, weight: UIFont.Weight) -> UIFontDescriptor {
var attributes = [UIFontDescriptor.AttributeName : Any]()
var traits = (attributes[.traits] as? [UIFontDescriptor.TraitKey: Any]) ?? [:]
traits[.weight] = weight
attributes[.name] = nil
attributes[.traits] = traits
attributes[.family] = family
return UIFontDescriptor(fontAttributes: attributes)
}
func with(family: String, weight: UIFont.Weight) -> UIFontDescriptor {
var attributes = fontAttributes
var traits = (attributes[.traits] as? [UIFontDescriptor.TraitKey: Any]) ?? [:]
traits[.weight] = weight
attributes[.name] = nil
attributes[.traits] = traits
attributes[.family] = family
return UIFontDescriptor(fontAttributes: attributes)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment