Basic typography design system for iOS using Swift. Article link: http://www.popcornomnom.com/basic-typography-design-system-for-ios-using-swift/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Basic typography design system for
iOS
usingSwift
andSwiftUI
Detailed article