Sets the color, gradient, or material for text and symbols in a view hierarchy.
96.8% of iOS users can use this modifier
iOS 15+ required
.foregroundStyle(_:) Sets the primary foreground style
style: ShapeStyle.foregroundStyle(_:_:) Sets primary and secondary foreground styles
primary: ShapeStylesecondary: ShapeStyle.foregroundStyle(_:_:_:) Sets primary, secondary, and tertiary styles
primary: ShapeStylesecondary: ShapeStyletertiary: ShapeStyleSet text color using foregroundStyle
Text("Hello, World!")
.foregroundStyle(.blue)Apply a gradient to text
Text("Hello, World!")
.font(.largeTitle)
.foregroundStyle(
LinearGradient(
colors: [.blue, .purple],
startPoint: .leading,
endPoint: .trailing
)
)Set primary and secondary colors for SF Symbols
Label("Settings", systemImage: "gear")
.foregroundStyle(.primary, .secondary)Use foregroundStyle with fallback for iOS 14 and earlier
Text("Hello, World!")
.modifier(StyledText())
struct StyledText: ViewModifier {
func body(content: Content) -> some View {
if #available(iOS 15, *) {
content.foregroundStyle(.blue)
} else {
content.foregroundColor(.blue)
}
}
}Loading related modifiers...
If your app supports iOS versions older than 15, you can use #available to conditionally apply this modifier:
if #available(iOS 15, *) {
myView.foregroundStyle()
} else {
// Fallback for older iOS versions
myView
}Check the code examples above for specific #available patterns with this modifier.
The .foregroundStyle() modifier requires iOS 15 or later. This means 96.8% of current iOS users can use this modifier.
Yes! Use #available(iOS 15, *) to conditionally apply this modifier only on devices running iOS 15 or later, with a fallback for older versions.
Apply the .foregroundStyle() modifier to any SwiftUI view. Check the code examples above for usage patterns.
Visit the official Apple documentation for detailed information.