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

  • Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.
    • Includes 7 Courses
    • With Certificate
    • Beginner Friendly.
      13 hours
  • A powerful programming language developed by Apple for iOS, macOS, and more.
    • Beginner Friendly.
      12 hours

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

  • Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.
    • Includes 7 Courses
    • With Certificate
    • Beginner Friendly.
      13 hours
  • A powerful programming language developed by Apple for iOS, macOS, and more.
    • Beginner Friendly.
      12 hours