Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Demo code of building typography system in SwiftUI
struct TokenTypography {
// 1. Prepare base materials
/// a. Level 1 base settings
private enum FontSize: CGFloat {
case
small = 12,
medium = 17,
large = 28
}
private enum FontFamily: String {
case
HelveticaNeue = "HelveticaNeue",
Georgia = "Georgia"
}
/// b. Level 2 tokens
enum FontSizeToken: CGFloat {
case caption, body, title
func getValue() -> CGFloat {
switch self {
case .caption:
return FontSize.small.rawValue
case .body:
return FontSize.medium.rawValue
case .title:
return FontSize.large.rawValue
}
}
}
enum FontFamilyToken: String {
case main, sub
func getValue() -> String {
switch self {
case .main:
return FontFamily.HelveticaNeue.rawValue
case .sub:
return FontFamily.Georgia.rawValue
}
}
}
// 2. Expose data
let mainFont: Font!
let subFont: Font!
init() {
self.mainFont = Font.custom(FontFamilyToken.main.getValue(), size: FontSizeToken.body.getValue())
self.subFont = Font.custom(FontFamilyToken.sub.getValue(), size: FontSizeToken.caption.getValue())
}
}
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.