Skip to content

Instantly share code, notes, and snippets.

@popcornomnom
Last active December 30, 2021 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save popcornomnom/e7c3f5e792627ad0423ba9ee1c2f7641 to your computer and use it in GitHub Desktop.
Save popcornomnom/e7c3f5e792627ad0423ba9ee1c2f7641 to your computer and use it in GitHub Desktop.
Basic typography design system for iOS using Swift. Article link: http://www.popcornomnom.com/basic-typography-design-system-for-ios-using-swift/
//
// FontManager.swift
//
// Created by http://www.popcornomnom.com
// Copyright © 2019 Marharyta Lytvynenko. All rights reserved.
//
import UIKit
import SwiftUI
//MARK: - Font Parts
public extension UIFont {
enum Family: String {
case system = ".SFUIText" //".SFUI"
case inter = "Inter"
//easy to change default app fonts 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 = 15, bodyS = 13
}
//put Family and Weight together
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 = FontWeight.semibold.rawValue
case (.inter, .light): fontWeight = "\(weight.rawValue)BETA"
default: fontWeight = weight.rawValue
}
let familyName = family.rawValue
return fontWeight.isEmpty ? "\(familyName)" : "\(familyName)-\(fontWeight)"
}
}
//MARK: - Initializers
extension UIFont {
convenience init(_ size: FontSize, _ weight: FontWeight) {
self.init(.defaultFamily, size, weight)
}
convenience init(_ family: FontFamily = .defaultFamily,
_ size: FontSize, _ weight: FontWeight) {
self.init(name: stringName(family, weight), size: size.rawValue)!
}
}
//MARK: - SwiftUI
@available(iOS 13.0, *)
extension Font {
init(_ size: FontSize, _ weight: FontWeight) {
self.init(.defaultFamily, size, weight)
}
init(_ family: FontFamily = .defaultFamily,
_ size: FontSize, _ weight: FontWeight) {
self.init(UIFont(family, size, weight))
}
}
@popcornomnom
Copy link
Author

Basic typography design system for iOS using Swift and SwiftUI

Detailed article

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