ButtonStyle & LabelStyle
Build branded, reusable control styles.
ButtonStyle & LabelStyle is a free SwiftUI Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Style Controls, Not Instances
Restyling every button by hand gets tedious. A ButtonStyle defines one branded look you reuse across the whole app. 🎨
The ButtonStyle Protocol
Conform a struct to ButtonStyle and implement makeBody. It receives a configuration and returns the styled button.
struct PrimaryStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
}
}The label Inside Configuration
The configuration gives you the buttons label, which is the title or content. You decorate that label with padding and color.
configuration.label
.padding()
.background(.blue)React to isPressed
Configuration also exposes isPressed. Use it to dim or scale the button while a finger is down for tactile feedback.
configuration.label
.opacity(configuration.isPressed ? 0.6 : 1)Apply with buttonStyle
Attach your style with the .buttonStyle modifier. Every button it touches adopts the same branded appearance.
Button("Save") { }
.buttonStyle(PrimaryStyle())Style a Whole Container
Set .buttonStyle on a stack and every button inside inherits it. One line styles a full group of controls.
Built-in Styles Exist Too
SwiftUI ships ready styles like .bordered and .borderedProminent. Reach for those before writing a custom one.
Button("Go") { }
.buttonStyle(.borderedProminent)Now Meet LabelStyle
A Label pairs text with an icon. A LabelStyle controls how that pair is arranged and shown.
Label("Home", systemImage: "house")Title-Only or Icon-Only
Built-in label styles let you show just the title, just the icon, or both. Switch based on space without changing the call site.
Label("Home", systemImage: "house")
.labelStyle(.iconOnly)Custom LabelStyle
Conform to LabelStyle and implement makeBody to rearrange the icon and title yourself, like stacking them vertically.
func makeBody(configuration: Configuration) -> some View {
VStack { configuration.icon; configuration.title }
}One Definition, Many Buttons
Custom styles keep look and behavior in one place. Tweak the style and every button or label across the app updates together.
Quick Check
Quick check on control styles.
Recap: Reusable Control Looks
Use ButtonStyle and LabelStyle to define branded controls once. React to isPressed, rearrange labels, and apply the look everywhere. 🧠
Frequently asked questions
Is the “ButtonStyle & LabelStyle” lesson free?
Yes — the full text of “ButtonStyle & LabelStyle” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.
What will I learn in “ButtonStyle & LabelStyle”?
Build branded, reusable control styles. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SwiftUI Academy?
No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “ButtonStyle & LabelStyle” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SwiftUI Academy lesson?
Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Extracting Reusable Views
- Custom ViewModifiers
- ButtonStyle & LabelStyle
- ViewBuilder & Generic Containers