How to Correctly use .secondary Hierarchy in SwiftUI

Discover how to apply hierarchical styling to text, buttons, shapes, images and labels. Improve your UI and UX with this styling.

Francesco Leoni

1 min read

Availability

iOS 15

macOS 12

tvOS 15

watchOS 8

Apply the styling

The Text view allows us to display strings in SwiftUI.

SwiftUI provides many modifiers to style it according to what we need and today we will focus on one style that can be applied.

The hierarchical style.

This property can be applied using the .foregroundStyle(_) modifier.

It has 5 levels: .primary, .secondary, .tertiary, .quaternary and .quinary.

VStack(alignment: .center) {

Text("Primary")

.foregroundStyle(.primary)

Text("Secondary")

.foregroundStyle(.secondary)

Text("Tertiary")

.foregroundStyle(.tertiary)

Text("Quaternary")

.foregroundStyle(.quaternary)

Text("Quinary")

.foregroundStyle(.quinary)

}

.foregroundStyle(.purple)

Here the .secondary style dims the foregroundColor applied to the VStack.

Screenshot-2023-11-21-at-16.45.51.webp

Compatibility

The hierarchical styles can dim every custom color, not only SwiftUI default ones.

And it works not only with Text but also with Shape, Button, Stacks, Image and Label.

Screenshot-2023-11-21-at-16.46.17.webp

Label

Label("Secondary", systemImage: "2.square.fill")

.foregroundStyle(.secondary)

Shape

RoundedRectangle(cornerRadius: 15)

.frame(width: 100, height: 30)

.foregroundStyle(.secondary)

Image

Image(systemName: "2.square.fill")

.foregroundStyle(.secondary)

Button

Button("Tap here!") {

// Perform action

}

.foregroundStyle(.secondary)

If you have any question about this article, feel free to email me or tweet me @franceleonidev and share your opinion.

Thank you for reading and see you in the next article!

Share this article

Related articles


Combine CoreData and SwiftUI

See how to use CoreData database with SwiftUI. Syncing changes from CoreData to every View of your app.

5 min read

SwiftUICoreData

SwiftData the Successor of CoreData Explained with SwiftUI

Discover the SwiftData framework built on top of CoreData. Save and fetch data locally. Available for Xcode 15, Swift and SwiftUI.

5 min read

SwiftUICoreData

Make your Chart Scrollable with SwiftUI Charts (iOS 17)

Discover the new SwiftUI Charts APIs that enables you to create scrollable chart easily. Available for Xcode 15 and iOS 17.

2 min read

ChartsSwiftUI