Skip to main content

How to provide visual structure using foreground styles

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to provide visual structure using foreground styles 관련

SwiftUI by Example

Back to Home

How to provide visual structure using foreground styles | SwiftUI by Example

How to provide visual structure using foreground styles

Updated for Xcode 15

New in iOS 15

SwiftUI provides a foregroundStyle() modifier to control the way text, images, and shapes are styled all at once. In its simplest form this is similar to using foregroundStyle() with .secondary, but not only does it unlock more of the semantic colors – .tertiary and .quaternary, it also adds support for anything that conforms to ShapeStyle.

So, here's an example of an image and some text rendered using quaternary style, which is the lowest of four importance levels for content:

HStack {
    Image(systemName: "clock.fill")
    Text("Set the time")
}
.font(.largeTitle.bold())
.foregroundStyle(.quaternary)

Download this as an Xcode projectopen in new window

A clock symbol beside the words “Set the time”, both in faint grey.

Like I said, anything that conforms to ShapeStyle also works, meaning that we can render our HStack with a red to black linear gradient using the same modifier:

HStack {
    Image(systemName: "clock.fill")
    Text("Set the time")
}
.font(.largeTitle.bold())
.foregroundStyle(
    .linearGradient(
        colors: [.red, .black],
        startPoint: .top,
        endPoint: .bottom
    )
)

Download this as an Xcode projectopen in new window

A clock symbol beside the words “Set the time”, both with a gradient running from red at the top to black at the bottom.
A clock symbol beside the words “Set the time”, both with a gradient running from red at the top to black at the bottom.
Similar solutions…
How to detect when your app moves to the background or foreground with scenePhase | SwiftUI by Example

How to detect when your app moves to the background or foreground with scenePhase
How to add visual effect blurs | SwiftUI by Example

How to add visual effect blurs
How to provide relative sizes using GeometryReader | SwiftUI by Example

How to provide relative sizes using GeometryReader
How to get custom colors and transparency with SF Symbols | SwiftUI by Example

How to get custom colors and transparency with SF Symbols
SwiftUI tips and tricks | SwiftUI by Example

SwiftUI tips and tricks

이찬희 (MarkiiimarK)
Never Stop Learning.