Skip to content

Instantly share code, notes, and snippets.

@popcornomnom
Last active June 13, 2020 13:46
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 popcornomnom/faa5aae21b9f958fe7b45020e90b57cc to your computer and use it in GitHub Desktop.
Save popcornomnom/faa5aae21b9f958fe7b45020e90b57cc to your computer and use it in GitHub Desktop.
//
// FontManager.swift
//
// Created by http://www.popcornomnom.com
// Copyright © 2019 Marharyta Lytvynenko. All rights reserved.
//
import UIKit
//MARK: - Font Parts
public extension UIFont {
enum Family: String {
case system = ".SFUIText" //".SFUI"
case inter = "Inter"
//easy to change default app font family
static let defaultFamily = Family.inter
}
enum CustomWeight: String {
case regular = "", medium, light, heavy, bold, semibold, black
}
enum Size: CGFloat {
case h1 = 36, h2 = 28, h3 = 20
case bodyL = 17, bodyM = 14, bodyS = 12
}
private class func stringName(_ family: Family, _ weight: CustomWeight) -> String {
/**
Define incompatible family, weight here
in this case set defaults compatible values
*/
let fontWeight: String
switch (family, weight) {
case (.inter, .heavy):
fontWeight = CustomWeight.semibold.rawValue
case (.inter, .light):
fontWeight = "\(weight.rawValue)BETA"
default:
fontWeight = weight.rawValue
}
//put Family and Weight together
let familyName = family.rawValue
return fontWeight.isEmpty ? "\(familyName)" : "\(familyName)-\(fontWeight)"
}
}
//MARK: - Initializers
public extension UIFont {
convenience init(_ size: Size, _ weight: CustomWeight) {
self.init(.defaultFamily, size, weight)
}
convenience init(_ family: Family = .defaultFamily,
_ size: Size, _ weight: CustomWeight) {
self.init(name: UIFont.stringName(family, weight), size: size.rawValue)!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment