.foregroundColor()

awgraves's avatar
Published Feb 27, 2023Updated Mar 3, 2023
Contribute to Docs

The .foregroundColor() modifier sets the foreground color displayed in a view.

Syntax

Text("We are setting the foreground color to green!")
    .foregroundColor(color)

The .foregroundColor() applies the color to the Text which conforms to the View protocol.

Example

struct ContentView: View {
var body: some View {
Text("We are setting the foreground color to green!")
.foregroundColor(.green)
}
}

In the above example, the .foregroundColor() modifier with the .green parameter is called on the Text view. This applies the color green to all characters.

This will display the following:

SwiftUI ViewModifier .foregroundColor()

All contributors

Contribute to Docs

Learn SwiftUI on Codecademy