Color
A representation of a color that adapts to a given context.
Declaration
type Color =
| 'accentColor'
| 'black'
| 'blue'
| 'brown'
| 'clear'
| 'cyan'
| 'gray'
| 'green'
| 'indigo'
| 'mint'
| 'orange'
| 'pink'
| 'primary'
| 'purple'
| 'red'
| 'secondary'
| 'teal'
| 'white'
| 'yellow'
| { colorSpace?: ColorRGBColorSpace; red: number; green: number; blue: number; opacity?: number; }
| { colorSpace?: ColorRGBColorSpace; white: number; opacity?: number; }
| { hue: number; saturation: number; brightness: number; opacity?: number; }
| { name: string; }
| ColorResolved
| ColorResolvedHDR
| string;@frozen struct ColorPass a literal, or build one with SwiftUI.Color from import { SwiftUI } from 'swiftui-react-native'.
Type properties
'accentColor'SwiftUI.Color.accentColorA color that reflects the accent color of the system or app.
'black'SwiftUI.Color.blackA black color suitable for use in UI elements.
'blue'SwiftUI.Color.blueA context-dependent blue color suitable for use in UI elements.
'brown'SwiftUI.Color.brownA context-dependent brown color suitable for use in UI elements.
'clear'SwiftUI.Color.clearA clear color suitable for use in UI elements.
'cyan'SwiftUI.Color.cyanA context-dependent cyan color suitable for use in UI elements.
'gray'SwiftUI.Color.grayA context-dependent gray color suitable for use in UI elements.
'green'SwiftUI.Color.greenA context-dependent green color suitable for use in UI elements.
'indigo'SwiftUI.Color.indigoA context-dependent indigo color suitable for use in UI elements.
'mint'SwiftUI.Color.mintA context-dependent mint color suitable for use in UI elements.
'orange'SwiftUI.Color.orangeA context-dependent orange color suitable for use in UI elements.
'pink'SwiftUI.Color.pinkA context-dependent pink color suitable for use in UI elements.
'primary'SwiftUI.Color.primaryThe color to use for primary content.
'purple'SwiftUI.Color.purpleA context-dependent purple color suitable for use in UI elements.
'red'SwiftUI.Color.redA context-dependent red color suitable for use in UI elements.
'secondary'SwiftUI.Color.secondaryThe color to use for secondary content.
'teal'SwiftUI.Color.tealA context-dependent teal color suitable for use in UI elements.
'white'SwiftUI.Color.whiteA white color suitable for use in UI elements.
'yellow'SwiftUI.Color.yellowA context-dependent yellow color suitable for use in UI elements.
Type methods
Overview
You can create a color in one of several ways:
-
Load a color from an Asset Catalog:
let aqua = Color("aqua") // Looks in your app's main bundle by default. -
Specify component values, like red, green, and blue; hue, saturation, and brightness; or white level:
let skyBlue = Color(red: 0.4627, green: 0.8392, blue: 1.0) let lemonYellow = Color(hue: 0.1639, saturation: 1, brightness: 1) let steelGray = Color(white: 0.4745) -
Create a color instance from another color, like a doc://com.apple.documentation/documentation/UIKit/UIColor or an doc://com.apple.documentation/documentation/AppKit/NSColor:
#if os(iOS) let linkColor = Color(uiColor: .link) #elseif os(macOS) let linkColor = Color(nsColor: .linkColor) #endif -
Use one of a palette of predefined colors, like
ShapeStyle.black,ShapeStyle.green, andShapeStyle.purple.
Some view modifiers can take a color as an argument. For example,
View.foregroundStyle(_:) uses the color you provide to set the
foreground color for view elements, like text or
doc://com.apple.documentation/design/human-interface-guidelines/sf-symbols:
Image(systemName: "leaf.fill")
.foregroundStyle(Color.green)Because SwiftUI treats colors as View instances, you can also
directly add them to a view hierarchy. For example, you can layer
a rectangle beneath a sun image using colors defined above:
ZStack {
skyBlue
Image(systemName: "sun.max.fill")
.foregroundStyle(lemonYellow)
}
.frame(width: 200, height: 100)A color used as a view expands to fill all the space it's given,
as defined by the frame of the enclosing ZStack in the above example:
SwiftUI only resolves a color to a concrete value just before using it in a given environment. This enables a context-dependent appearance for system defined colors, or those that you load from an Asset Catalog. For example, a color can have distinct light and dark variants that the system chooses from at render time.
From Apple’s SwiftUI documentation. Code samples there are Swift.
Used by
- AngularGradientAn angular gradient.
- ColorA representation of a color that adapts to a given context.
- ColorPickerA control used to select a color from the system color picker UI.
- EllipticalGradientA radial gradient that draws an ellipse.
- LinearGradientA linear gradient.
- MeshGradientA two-dimensional gradient defined by a 2D grid of positioned colors.
- RadialGradientA radial gradient.
- mixReturns a version of self mixed with rhs by the amount specified by fraction.
- colorMultiplyAdds a color multiplication effect to this view.
- listItemTintSets the tint effect for content in a list.
- listRowSeparatorTintSets the tint color associated with a row.
- listSectionSeparatorTintSets the tint color associated with a section.
- shadowAdds a shadow to this view.
- strikethroughApplies a strikethrough to the text in this view.
- strikethroughApplies a strikethrough to the text.
- underlineApplies an underline to the text.
- tintSets the tint color within this view.
- underlineApplies an underline to the text in this view.
See also
More in Appearance.
- BackgroundProminenceThe prominence of backgrounds underneath other views.
- BadgeProminenceThe visual prominence of a badge.
- ButtonSizingThe sizing behavior of Buttons and other button-like controls.
- ColorRenderingModeThe set of possible working color spaces for color-compositing operations.
- ColorResolvedA concrete color value.
- ColorResolvedHDRA concrete color value, including HDR headroom information.
- ColorRGBColorSpaceA profile that specifies how to interpret a color value for display.
- ColorSchemeThe possible color schemes, corresponding to the light and dark appearances.
- GlassA structure that defines the configuration of a physical glass material.
- GlassChainGlass with chained calls: SwiftUI.Glass.interactive(...).
- ImageScaleA scale to apply to vector images relative to text.
- ProminenceA type indicating the prominence of a view hierarchy.