Design Tokens in SwiftUI: Building a Theme.swift That Scales
Every SwiftUI app starts with .foregroundStyle(.blue) and .padding(16) sprinkled through its views. It works until you want to change the accent color, support dark mode properly, or hand the codebase to an AI agent that will happily invent a fourteenth shade of gray. The fix is old and boring and works: design tokens, defined once, used everywhere.
This post builds a Theme.swift you can copy: colors that adapt to light and dark mode, named type roles, a spacing scale, radii, and a couple of shared components.
Colors that adapt to light and dark mode
You can put colors in an asset catalog, but keeping them in code makes them searchable, reviewable in a diff, and easy for an agent to read. The trick is a Color initializer that picks a value per appearance, built on UIColor's dynamic provider:
import SwiftUI
extension Color {
init(light: UInt32, dark: UInt32) {
self.init(uiColor: UIColor { traits in
UIColor(rgb: traits.userInterfaceStyle == .dark ? dark : light)
})
}
}
extension UIColor {
convenience init(rgb: UInt32) {
self.init(
red: CGFloat((rgb >> 16) & 0xFF) / 255,
green: CGFloat((rgb >> 8) & 0xFF) / 255,
blue: CGFloat(rgb & 0xFF) / 255,
alpha: 1
)
}
}Then name colors by role, not by hue:
enum Theme {
static let surface = Color(light: 0xF4F1EA, dark: 0x111110)
static let surfaceRaised = Color(light: 0xFBF9F4, dark: 0x1A1A18)
static let onSurface = Color(light: 0x17160F, dark: 0xEDEAE0)
static let onSurfaceVariant = Color(light: 0x5E5A4E, dark: 0xA8A495)
static let accent = Color(light: 0x0E5C46, dark: 0x3FA37F)
static let positive = Color(light: 0x0E7A4F, dark: 0x4CC38A)
static let negative = Color(light: 0xC23B22, dark: 0xF07A62)
}Theme.onSurfaceVariant says where a color belongs. When the design changes, you change one line, and dark mode is designed rather than inverted.
Type roles
Same idea for type: a handful of named roles instead of ad-hoc sizes. Building them on the system text styles keeps Dynamic Type working:
extension Theme {
enum Typeface {
static let display = Font.system(.largeTitle, design: .default, weight: .bold)
static let title = Font.system(.title2, weight: .semibold)
static let headline = Font.system(.headline)
static let body = Font.system(.body)
static let caption = Font.system(.caption)
/// Amounts and stats: tabular digits so columns line up.
static let numeral = Font.system(.title, weight: .semibold).monospacedDigit()
}
}If your design uses fixed sizes, Font.custom(_:size:relativeTo:) still scales with Dynamic Type.
A closed spacing scale and radii
A spacing scale is the token most often skipped and the one that matters most for "does this look designed". Make it closed, so there's no 13:
extension Theme {
enum Space {
static let xs: CGFloat = 4
static let sm: CGFloat = 8
static let md: CGFloat = 12
static let lg: CGFloat = 16
static let xl: CGFloat = 24
static let xxl: CGFloat = 32
/// Leading and trailing screen margin.
static let margin: CGFloat = 20
}
enum Radius {
static let sm: CGFloat = 8
static let md: CGFloat = 14
static let lg: CGFloat = 22
}
}Motion tokens
Animations drift just as fast as colors. Name the two or three curves your app uses:
extension Theme {
enum Motion {
static let quick = Animation.snappy(duration: 0.2)
static let standard = Animation.smooth(duration: 0.35)
static let playful = Animation.bouncy(duration: 0.45)
}
}Shared components on top of tokens
Tokens pay off when the components built from them live in the same file. A primary button and a card, for example:
struct PrimaryButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
.font(Theme.Typeface.headline)
.foregroundStyle(.white)
.frame(maxWidth: .infinity, minHeight: 52)
.background(Theme.accent, in: .rect(cornerRadius: Theme.Radius.md))
.opacity(configuration.isPressed ? 0.85 : 1)
.scaleEffect(configuration.isPressed ? 0.98 : 1)
.animation(Theme.Motion.quick, value: configuration.isPressed)
}
}
extension ButtonStyle where Self == PrimaryButtonStyle {
static var primary: PrimaryButtonStyle { PrimaryButtonStyle() }
}
struct Card<Content: View>: View {
@ViewBuilder var content: Content
var body: some View {
content
.padding(Theme.Space.lg)
.frame(maxWidth: .infinity, alignment: .leading)
.background(Theme.surfaceRaised, in: .rect(cornerRadius: Theme.Radius.lg))
}
}Now a screen reads like the design spec:
struct BalanceView: View {
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: Theme.Space.xl) {
Card {
VStack(alignment: .leading, spacing: Theme.Space.xs) {
Text("Total balance")
.font(Theme.Typeface.caption)
.foregroundStyle(Theme.onSurfaceVariant)
Text("$24,318.62")
.font(Theme.Typeface.numeral)
.foregroundStyle(Theme.onSurface)
}
}
Button("Add money") {}
.buttonStyle(.primary)
}
.padding(.horizontal, Theme.Space.margin)
}
.background(Theme.surface)
}
}Keeping it honest
- Ban raw values in views. A quick search for
Color(red:,.padding(1and.font(.system(size:in review catches most leaks. - Preview both appearances. Two
#Previews with.preferredColorScheme(.light)and.darkper screen. - Keep the spec next to the code. A DESIGN.md holds the values and the reasoning, and
Theme.swiftis its translation. Change both together.
Skip the blank page
Every Effect UI design style ships with a finished Theme.swift: tokens and shared components for its DESIGN.md, in light and dark mode, plus twelve ready-made screens that use it.