How to provide visual structure using foreground styles
About 2 min
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)
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
)
)
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