foregroundStyle

Sets the style of the text displayed by this view.

Text ModifieriOS 17.0+macOS 14.0+tvOS 17.0+watchOS 10.0+foregroundStyle(_:) in SwiftUI

Declaration

React
compose().foregroundStyle(style: SwiftUI.ShapeStyle)
Swift
nonisolated func foregroundStyle<S>(
    _ style: S
) -> Text
where S : ShapeStyle

Parameters

Return value

A text view that uses the color value you supply.

Discussion

Use this method to change the rendering style of the text rendered by a text view.

For example, you can display the names of the colors red, green, and blue in their respective colors:

HStack {
    Text("Red").foregroundStyle(.red)
    Text("Green").foregroundStyle(.green)
    Text("Blue").foregroundStyle(.blue)
}

See also

More in Text modifiers.