Updated for Xcode 14.0 beta 1
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 foregroundColor()
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 project
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 project
SPONSORED In-app subscriptions are a pain. The code can be hard to write, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.