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
enum FontFamily: String {
case system = ".SFUIText" //".SFUI"
case inter = "Inter"
//easy to change default app fonts family
static let defaultFamily = FontFamily.inter
}
enum FontWeight: String {
case regular = "", medium, light, heavy, bold, semibold, black
}
enum FontSize: CGFloat {
case h1 = 36, h2 = 28, h3 = 20
case bodyL = 17, bodyM = 15, bodyS = 13
case tabItem = 10
}
//put Family and Weight together
private func stringName(_ family: FontFamily, _ weight: FontWeight) -> 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
default:
fontWeight = weight.rawValue
}
let familyName = family.rawValue
return fontWeight.isEmpty ? "\(familyName)" : "\(familyName)-\(fontWeight)"
}
//MARK: -
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 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.