.foregroundStyle()

iOS 15+

Sets the color, gradient, or material for text and symbols in a view hierarchy.

Visual Effects
Official Apple Documentation →
iOS User Coverage
96.8%
coverage

96.8% of iOS users can use this modifier

Version Compatibility
iOS 18.7
30.3% Supported
iOS 18.6
29.8% Supported
iOS 26.1
10.7% Supported
iOS 18.5
6.0% Supported
iOS 16.7
2.3% Supported
iOS 26.2
2.0% Supported
iOS 18.3
1.8% Supported
iOS 15.8
1.8% Supported
iOS 17.6
1.7% Supported
iOS 26.0
1.3% Supported

iOS 15+ required

Variants
.foregroundStyle(_:)

Sets the primary foreground style

Parameters:
  • style: ShapeStyle
.foregroundStyle(_:_:)

Sets primary and secondary foreground styles

Parameters:
  • primary: ShapeStyle
  • secondary: ShapeStyle
.foregroundStyle(_:_:_:)

Sets primary, secondary, and tertiary styles

Parameters:
  • primary: ShapeStyle
  • secondary: ShapeStyle
  • tertiary: ShapeStyle
Code Examples

Solid Color

Set text color using foregroundStyle

Text("Hello, World!")
    .foregroundStyle(.blue)

Gradient Foreground

Apply a gradient to text

Text("Hello, World!")
    .font(.largeTitle)
    .foregroundStyle(
        LinearGradient(
            colors: [.blue, .purple],
            startPoint: .leading,
            endPoint: .trailing
        )
    )

Hierarchical Styles

Set primary and secondary colors for SF Symbols

Label("Settings", systemImage: "gear")
    .foregroundStyle(.primary, .secondary)

Conditional Availability (iOS 15+)

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...

Using with Older iOS Versions

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.

Frequently Asked Questions

What iOS version is required for .foregroundStyle()?

The .foregroundStyle() modifier requires iOS 15 or later. This means 96.8% of current iOS users can use this modifier.

Can I use this if my app supports older iOS versions?

Yes! Use #available(iOS 15, *) to conditionally apply this modifier only on devices running iOS 15 or later, with a fallback for older versions.

How do I use .foregroundStyle() in SwiftUI?

Apply the .foregroundStyle() modifier to any SwiftUI view. Check the code examples above for usage patterns.

Where can I learn more?

Visit the official Apple documentation for detailed information.

Explore More SwiftUI Modifiers