Hard-Coded Hex and Rogue Padding: Auditing Your SwiftUI Design System Before the Redesign Lands
Photo: Einsgoeins, CC BY-SA 4.0, via Wikimedia Commons
Every design system starts as a promise and ends up as an archaeology project. Ours was no different. Somewhere between the third contractor and the second rebrand, the SwiftUI codebase picked up seven shades of gray, four different "large" corner radii, and a text style called .headline2 that nobody on the team could explain without checking git blame.
None of that is unusual. What's unusual is how long teams let it slide — usually right up until a redesign lands and someone cheerfully asks, "So how many files are we touching?" The honest answer is all of them, twice, plus a hotfix release because the checkout button turned the wrong blue on iPad.
The fix isn't a heroic rewrite. It's a boring audit you run before the redesign, while the stakes are low and the design team is still arguing about the new palette.
Token drift is an API problem in a trench coat
Here's the mental shift that makes this whole thing tractable: your design tokens are an API. The names (AppColor.textSecondary, Spacing.md) are the public surface. The SwiftUI views are the consumers. A rename is a breaking change. A hard-coded .padding(17) is a view that decided to fork the contract and go its own way.
Once you frame it that way, the questions get sharper. Who consumes this token? Can I deprecate it without a coordinated migration? Is there a version of this value that only one screen needs, and if so, why is it in the shared file?
Most teams never ask any of that, which is exactly how you end up with 40 hex codes typed directly into .foregroundStyle calls across the app.
Step one: inventory every hard-coded value
Don't open Xcode and start scrolling. You'll lose an afternoon and find maybe a third of the offenders. Use the tools that already exist.
# Raw color constructors
rg -n --type swift 'Color\(red:|UIColor\(red:' Sources/
rg -n 'Color\(hex:' Sources/
rg -n '#colorLiteral' Sources/
# Magic padding and spacing
rg -n '\.padding\([0-9]' Sources/
rg -n '\.frame\([^)]*[0-9]{2,}' Sources/
# Typography that bypassed the type ramp
rg -n '\.font\(\.system\(size:' Sources/
# Corner radii
rg -n 'cornerRadius: *[0-9]|RoundedRectangle\(cornerRadius: *[0-9]' Sources/
Dump the output into a spreadsheet — yes, really — and sort by frequency. In almost every codebase I've poked at, the top 50 to 60 values cover roughly 90 percent of the hits. That long tail is where you'll find one-off values that exist because a designer eyeballed something on a 27-inch display in 2021 and nobody has touched it since.
Label each row with a guess: primitive (a raw value like #1F2937), semantic (what it's used for), or orphan (used once, probably dead). You don't need to be right. You need a map.
Step two: sort primitives from semantics — but only the ones you use
The classic mistake here is to build a gorgeous three-layer token system with 400 primitives and 18 theme scopes before you've migrated a single view. That's not an audit, that's a side project that will outlive the redesign.
Start with the smallest semantic layer that covers your actual usage:
enum AppColor {
static let textPrimary = Color(hex: 0x111214)
static let textSecondary = Color(hex: 0x5B6068)
static let surface = Color(hex: 0xFFFFFF)
static let accent = Color(hex: 0x0A66FF)
}
The critical property is that these names describe intent, not appearance. textSecondary survives a repaint. grayMedium doesn't — and that's the thing that makes old tokens impossible to delete.
Step three: migrate in slices, then put a fence around the old stuff
Do not open one 90-file pull request. It'll sit in review for a week, gather conflicts, and get merged at 11 p.m. on a Friday by the one person who still understands it.
Migrate one screen or one feature module per PR. Small diffs, fast reviews, easy reverts. Then add a lint rule so new code can't reintroduce the problem:
custom_rules:
no_raw_hex:
included: ".*\\.swift"
regex: 'Color\(hex:'
severity: error
message: "Use AppColor semantics instead of raw hex."
no_magic_padding:
included: ".*\\.swift"
regex: '\\.padding\\([0-9]'
severity: warning
message: "Use Spacing tokens."
Exclude your token definitions file and you're done. The fence is what stops the audit from quietly undoing itself over the next two quarters, which is the default outcome otherwise.
Step four: let snapshots and previews catch drift
A semantic token layer is only as good as your ability to see what changed. Two cheap tools cover most of it.
First, a token gallery preview — a single SwiftUI view that renders every color swatch, spacing step, and text style in a grid. It takes 20 minutes to write and it's the fastest way to spot a token that renders identically to another one (which usually means someone typed the wrong hex three releases ago).
Second, snapshot tests on components, not full screens:
func testTokenGalleryLight() {
assertSnapshot(of: TokenGallery(), as: .image(layout: .fixed(width: 390)))
}
func testTokenGalleryDark() {
assertSnapshot(of: TokenGallery(),
as: .image(layout: .fixed(width: 390),
traits: .init(userInterfaceStyle: .dark)))
}
Run them in light, dark, and at accessibility text sizes. Whole-screen snapshots are noisy and get blessed without looking. Component snapshots actually get reviewed.
Step five: deprecate like you mean it
Here's the part everyone skips. Don't delete a token the day you stop using it — announce it, in code, where the compiler will complain for you:
extension Color {
@available(*, deprecated, renamed: "AppColor.textSecondary")
static let grayMedium = AppColor.textSecondary
}
Let the warnings do the discovery work. Ship one release, watch which modules still trip the warning, migrate those, then delete. If a token survives two releases with no consumers, it was never a token — it was a rumor.
The Friday checklist
If you take nothing else from this, take the order of operations:
- Grep for hard-coded values before you decide anything.
- Build a semantic layer that covers what you actually use, not what a design system blog told you to build.
- Migrate per screen, small PRs, one at a time.
- Fence it with lint rules the same week.
- Snapshot the gallery, not the screens.
- Deprecate with
@available, then delete.
The redesign is coming whether or not you do this. The only question is whether it costs you three focused days or three miserable sprints. Design tokens are an API — and APIs deserve a deprecation plan before the new version ships, not after.