Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@popcornomnom popcornomnom commented Sep 18, 2019

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
You can’t perform that action at this time.