UIColor object stores color data and sometimes opacity (alpha value). Many methods in UIKit require you to specify color data using a
UIColor object; when drawing you use them to set the current fill and stroke colors. Color objects are immutable and can be used safely from multiple threads in your app.
- iOS 2.0+
- tvOS 2.0+
- watchOS 2.0+
UIColor objects can be created in a variety of ways:
UIKit provides standard system colors that you can use to match the colors of your own UI objects to those provided by UIKit. You can use these colors without needing to understand the underlying representation of color. See Creating System Colors.
UIKit provides definitions for very basic colors, such as red, orange, yellow, and so on. See Creating a UIColor Object with a Predefined Color.
You can create a custom color in a Core Graphics color space. See Creating a Custom UIColor Object in a Specific Color Space.
You can create a color object from another object, such as another
UIColorobject, a Core Graphics representation of a color, or a Core Image color. See Creating a UIColor Object from another Representation of Color.
You can create a color from a
UIImageobject, which is used to paint a repeating pattern instead of a solid color. See Creating a UIColor Object that Draws Using a Pattern.
The most common way to use a
UIColor object is to provide it to some other object in UIKit. For example, the
UIView class (and its descendants) include background and tint colors to affect how they are drawn onscreen.
Listing 1 sets the background and tint color of a view.
When performing custom drawing, a
UIColor object provides methods that set the fill or stroke colors of the current graphics context.
Listing 2 shows a simple example of custom drawing inside a view.
See Setting the Graphics Context’s Drawing Color for the complete list of functions.
Color and Color Spaces
UIColor object is typically represented internally as a Core Graphics color (
CGColor) in a Core Graphics color space (
CGColorSpace). There are methods and properties that return the underlying color data. See Retrieving Color Information. When creating a custom color, the underlying color space and the range of values for each color component vary based on the iOS version.
On versions of iOS before iOS 10, and for apps linked on a version of iOS prior to iOS 10, colors are created and returned in one of two color spaces:
These device color spaces correspond closely to the display characteristics of the sRGB color space. Color component values within these color spaces are represented by values between
1.0. When a color is created, the input parameters are clamped to fit within this color space.
If your app was linked on or after iOS 10 and when running on iOS 10 or later, colors are created in extended color spaces:
When working in an extended color space, color values are not clamped to fit inside the color gamut, meaning that component values may be less than
0.0 or greater than
1.0. When displayed on an sRGB display, such colors are outside the gamut and will not be rendered accurately. However, extended color spaces are useful working color spaces when you want a pixel format and representation that other color spaces can be easily converted into. For example, a color in the display P3 color space that is not within the sRGB color gamut can still be converted to an extended sRGB format. Some of its values will be outside of the
1.0 range. However, when displayed on a device with a P3 display gamut, it would still be rendered correctly.
To summarize, when you need to worry about color spaces, use these extended color spaces as working color spacs. When you need to worry about representing that color as closely as possible in a specific color space, convert the color from the extended color space into the target color space.
Most developers have no need to subclass
UIColor. The only time doing so might be necessary is if you require support for additional colorspaces or color models. If you do subclass, the properties and methods you add must also be safe to use from multiple threads.