UIColor Class Reference
| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/UIKit.framework |
| Availability | Available in iOS 2.0 and later. |
| Declared in | UIColor.h UIInterface.h |
Overview
A UIColor object represents color and sometimes opacity (alpha value). You can use UIColor objects to store color data, and during drawing you can use them to set the current fill and stroke colors.
Many methods in UIKit require you to specify color data using a UIColor object, and for general color needs it should be your main way of specifying colors. The color spaces used by this object are optimized for use on iOS-based devices and are therefore appropriate for most drawing needs. If you prefer to use Core Graphics colors and color spaces instead, however, you may do so.
Color objects are immutable and so it is safe to use them from multiple threads in your app.
Most developers should 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.
Tasks
Creating a UIColor Object from Component Values
-
+ colorWithWhite:alpha: -
+ colorWithHue:saturation:brightness:alpha: -
+ colorWithRed:green:blue:alpha: -
+ colorWithCGColor: -
+ colorWithPatternImage: -
+ colorWithCIColor: -
– colorWithAlphaComponent:
Initializing a UIColor Object
-
– initWithWhite:alpha: -
– initWithHue:saturation:brightness:alpha: -
– initWithRed:green:blue:alpha: -
– initWithCGColor: -
– initWithPatternImage: -
– initWithCIColor:
Creating a UIColor with Preset Component Values
-
+ blackColor -
+ darkGrayColor -
+ lightGrayColor -
+ whiteColor -
+ grayColor -
+ redColor -
+ greenColor -
+ blueColor -
+ cyanColor -
+ yellowColor -
+ magentaColor -
+ orangeColor -
+ purpleColor -
+ brownColor -
+ clearColor
System Colors
-
+ lightTextColor -
+ darkTextColor -
+ groupTableViewBackgroundColor -
+ viewFlipsideBackgroundColor -
+ scrollViewTexturedBackgroundColor -
+ underPageBackgroundColor
Retrieving Color Information
-
CGColorproperty -
CIColorproperty -
– getHue:saturation:brightness:alpha: -
– getRed:green:blue:alpha: -
– getWhite:alpha:
Drawing Operations
Properties
CGColor
The Quartz color reference that corresponds to the receiver’s color. (read-only)
Availability
- Available in iOS 2.0 and later.
Declared In
UIColor.hCIColor
The Core Image color associated with the receiver. (read-only)
Discussion
This property throws an exception if the color object was not initialized with a Core Image color.
Availability
- Available in iOS 5.0 and later.
Declared In
UIColor.hClass Methods
blackColor
Returns a color object whose grayscale value is 0.0 and whose alpha value is 1.0.
Return Value
The UIColor object.
Availability
- Available in iOS 2.0 and later.
Declared In
UIColor.hblueColor
Returns a color object whose RGB values are 0.0, 0.0, and 1.0 and whose alpha value is 1.0.
Return Value
The UIColor object.
Availability
- Available in iOS 2.0 and later.
Declared In
UIColor.hbrownColor
Returns a color object whose RGB values are 0.6, 0.4, and 0.2 and whose alpha value is 1.0.
Return Value
The UIColor object.
Availability
- Available in iOS 2.0 and later.
Declared In
UIColor.hclearColor
Returns a color object whose grayscale and alpha values are both 0.0.
Return Value
The UIColor object.
Availability
- Available in iOS 2.0 and later.
Declared In
UIColor.hcolorWithCGColor:
Creates and returns a color object using the specified Quartz color reference.
Parameters
- cgColor
A reference to a Quartz color.
Return Value
The color object. The color information represented by this object is in the native colorspace of the specified Quartz color.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIColor.hcolorWithCIColor:
Converts a Core Image color object to its UIColor equivalent
Parameters
- ciColor
The Core Image color to convert.
Return Value
The UIColor object corresponding to the specified Core Image color.
Availability
- Available in iOS 5.0 and later.
See Also
Declared In
UIColor.hcolorWithHue:saturation:brightness:alpha:
Creates and returns a color object using the specified opacity and HSB color space component values.
Parameters
- hue
The hue component of the color object in the HSB color space, specified as a value from 0.0 to 1.0.
- saturation
The saturation component of the color object in the HSB color space, specified as a value from 0.0 to 1.0.
- brightness
The brightness (or value) component of the color object in the HSB color space, specified as a value from 0.0 to 1.0.
- alpha
The opacity value of the color object, specified as a value from 0.0 to 1.0.
Return Value
The color object. The color information represented by this object is in the device RGB colorspace.
Discussion
Values below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted as 1.0.
Availability
- Available in iOS 2.0 and later.
Declared In
UIColor.hcolorWithPatternImage:
Creates and returns a color object using the specified image.
Parameters
- image
The image to use when creating the pattern color.
Return Value
The pattern color.
Discussion
You can use pattern colors to set the fill or stroke color just as you would a solid color. During drawing, the image in the pattern color is tiled as necessary to cover the given area.
By default, the phase of the returned color is 0, which causes the top-left corner of the image to be aligned with the drawing origin. To change the phase, make the color the current color and then use the CGContextSetPatternPhase function to change the phase.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIColor.hcolorWithRed:green:blue:alpha:
Creates and returns a color object using the specified opacity and RGB component values.
Parameters
- red
The red component of the color object, specified as a value from 0.0 to 1.0.
- green
The green component of the color object, specified as a value from 0.0 to 1.0.
- blue
The blue component of the color object, specified as a value from 0.0 to 1.0.
- alpha
The opacity value of the color object, specified as a value from 0.0 to 1.0.
Return Value
The color object. The color information represented by this object is in the device RGB colorspace.
Discussion
Values below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted as 1.0.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIColor.hcolorWithWhite:alpha:
Creates and returns a color object using the specified opacity and grayscale values.
Parameters
- white
The grayscale value of the color object, specified as a value from 0.0 to 1.0.
- alpha
The opacity value of the color object, specified as a value from 0.0 to 1.0.
Return Value
The color object. The color information represented by this object is in the device gray colorspace.
Discussion
Values below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted as 1.0.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIColor.hcyanColor
Returns a color object whose RGB values are 0.0, 1.0, and 1.0 and whose alpha value is 1.0.
Return Value
The UIColor object.
Availability
- Available in iOS 2.0 and later.
Declared In
UIColor.hdarkGrayColor
Returns a color object whose grayscale value is 1/3 and whose alpha value is 1.0.
Return Value
The UIColor object.
Availability
- Available in iOS 2.0 and later.
Declared In
UIColor.hdarkTextColor
Returns the system color used for displaying text on a light background.
Return Value
The UIColor object.
Availability
- Available in iOS 2.0 and later.
Declared In
UIInterface.hgrayColor
Returns a color object whose grayscale value is 0.5 and whose alpha value is 1.0.
Return Value
The UIColor object.
Availability
- Available in iOS 2.0 and later.
Declared In
UIColor.hgreenColor
Returns a color object whose RGB values are 0.0, 1.0, and 0.0 and whose alpha value is 1.0.
Return Value
The UIColor object.
Availability
- Available in iOS 2.0 and later.
Declared In
UIColor.hgroupTableViewBackgroundColor
Returns the system color used for the background of a grouped table.
Return Value
The UIColor object.
Availability
- Available in iOS 2.0 and later.
Declared In
UIInterface.hlightGrayColor
Returns a color object whose grayscale value is 2/3 and whose alpha value is 1.0.
Return Value
The UIColor object.
Availability
- Available in iOS 2.0 and later.
Declared In
UIColor.hlightTextColor
Returns the system color used for displaying text on a dark background.
Return Value
The UIColor object.
Availability
- Available in iOS 2.0 and later.
Declared In
UIInterface.hmagentaColor
Returns a color object whose RGB values are 1.0, 0.0, and 1.0 and whose alpha value is 1.0.
Return Value
The UIColor object.
Availability
- Available in iOS 2.0 and later.
Declared In
UIColor.horangeColor
Returns a color object whose RGB values are 1.0, 0.5, and 0.0 and whose alpha value is 1.0.
Return Value
The UIColor object.
Availability
- Available in iOS 2.0 and later.
Declared In
UIColor.hpurpleColor
Returns a color object whose RGB values are 0.5, 0.0, and 0.5 and whose alpha value is 1.0.
Return Value
The UIColor object.
Availability
- Available in iOS 2.0 and later.
Declared In
UIColor.hredColor
Returns a color object whose RGB values are 1.0, 0.0, and 0.0 and whose alpha value is 1.0.
Return Value
The UIColor object.
Availability
- Available in iOS 2.0 and later.
Declared In
UIColor.hscrollViewTexturedBackgroundColor
Returns the system pattern color used to render the area behind scrollable content.
Return Value
The UIColor object.
Availability
- Available in iOS 3.2 and later.
Declared In
UIInterface.hunderPageBackgroundColor
Returns the system pattern color used to render the background of a page.
Return Value
The UIColor object.
Availability
- Available in iOS 5.0 and later.
Declared In
UIInterface.hviewFlipsideBackgroundColor
Returns the system color used for the back side of a view while it is being flipped.
Return Value
The UIColor object.
Availability
- Available in iOS 2.0 and later.
Declared In
UIInterface.hwhiteColor
Returns a color object whose grayscale value is 1.0 and whose alpha value is 1.0.
Return Value
The UIColor object.
Availability
- Available in iOS 2.0 and later.
Declared In
UIColor.hyellowColor
Returns a color object whose RGB values are 1.0, 1.0, and 0.0 and whose alpha value is 1.0.
Return Value
The UIColor object.
Availability
- Available in iOS 2.0 and later.
Declared In
UIColor.hInstance Methods
colorWithAlphaComponent:
Creates and returns a color object that has the same color space and component values as the receiver, but has the specified alpha component.
Parameters
- alpha
The opacity value of the new
UIColorobject.
Return Value
The new UIColor object.
Discussion
A subclass with explicit opacity components should override this method to return a color with the specified alpha.
Availability
- Available in iOS 2.0 and later.
Declared In
UIColor.hgetHue:saturation:brightness:alpha:
Returns the components that make up the color in the HSB color space.
Parameters
- hue
On return, the hue component of the color object, specified as a value between
0.0and1.0.- saturation
On return, the saturation component of the color object, specified as a value between
0.0and1.0.- brightness
On return, the brightness component of the color object, specified as a value between
0.0and1.0.- alpha
On return, the opacity component of the color object, specified as a value between
0.0and1.0.
Return Value
YES if the color could be converted, NO otherwise.
Discussion
If the color is in a compatible color space, the color is converted into the HSB color space and its components are returned to your application. If the color is not in a compatible color space, the parameters are unchanged.
Availability
- Available in iOS 5.0 and later.
Declared In
UIColor.hgetRed:green:blue:alpha:
Returns the components that make up the color in the RGB color space.
Parameters
- red
On return, the red component of the color object, specified as a value between
0.0and1.0.- green
On return, the green component of the color object, specified as a value between
0.0and1.0.- blue
On return, the blue component of the color object, specified as a value between
0.0and1.0.- alpha
On return, the opacity component of the color object, specified as a value between
0.0and1.0.
Return Value
YES if the color could be converted, NO otherwise.
Discussion
If the color is in a compatible color space, the color is converted into RGB format and its components are returned to your application. If the color is not in a compatible color space, the parameters are unchanged.
Availability
- Available in iOS 5.0 and later.
Declared In
UIColor.hgetWhite:alpha:
Returns the grayscale components of the color.
Parameters
- white
On return, the grayscale component of the color object, specified as a value between
0.0and1.0.- alpha
On return, the opacity component of the color object, specified as a value between
0.0and1.0.
Return Value
YES if the color could be converted, NO otherwise.
Discussion
If the color is in a compatible color space, the color is converted into grayscale format and returned to your application. If the color is not in a compatible color space, the parameters are unchanged.
Availability
- Available in iOS 5.0 and later.
Declared In
UIColor.hinitWithCGColor:
Initializes and returns a color object using the specified Quartz color reference.
Parameters
- cgColor
A reference to a Quartz color.
Return Value
An initialized color object. The color information represented by this object is in the native colorspace of the specified Quartz color.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIColor.hinitWithCIColor:
Initializes and returns a color object using the specified Core Image color.
Parameters
- ciColor
The Core Image color to convert.
Return Value
An initialized color object.
Availability
- Available in iOS 5.0 and later.
Declared In
UIColor.hinitWithHue:saturation:brightness:alpha:
Initializes and returns a color object using the specified opacity and HSB color space component values.
Parameters
- hue
The hue component of the color object in the HSB color space, specified as a value from 0.0 to 1.0.
- saturation
The saturation component of the color object in the HSB color space, specified as a value from 0.0 to 1.0.
- brightness
The brightness (or value) component of the color object in the HSB color space, specified as a value from 0.0 to 1.0.
- alpha
The opacity value of the color object, specified as a value from 0.0 to 1.0.
Return Value
An initialized color object. The color information represented by this object is in the device RGB colorspace.
Discussion
Values below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted as 1.0.
Availability
- Available in iOS 2.0 and later.
Declared In
UIColor.hinitWithPatternImage:
Initializes and returns a color object using the specified Quartz color reference.
Parameters
- image
The image to use when creating the pattern color.
Return Value
The pattern color.
Discussion
You can use pattern colors to set the fill or stroke color just as you would a solid color. During drawing, the image in the pattern color is tiled as necessary to cover the given area.
By default, the phase of the returned color is 0, which causes the top-left corner of the image to be aligned with the drawing origin. To change the phase, make the color the current color and then use the CGContextSetPatternPhase function to change the phase.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIColor.hinitWithRed:green:blue:alpha:
Initializes and returns a color object using the specified opacity and RGB component values.
Parameters
- red
The red component of the color object, specified as a value from 0.0 to 1.0.
- green
The green component of the color object, specified as a value from 0.0 to 1.0.
- blue
The blue component of the color object, specified as a value from 0.0 to 1.0.
- alpha
The opacity value of the color object, specified as a value from 0.0 to 1.0.
Return Value
An initialized color object. The color information represented by this object is in the device RGB colorspace.
Discussion
Values below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted as 1.0.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIColor.hinitWithWhite:alpha:
Initializes and returns a color object using the specified opacity and grayscale values.
Parameters
- white
The grayscale value of the color object, specified as a value from 0.0 to 1.0.
- alpha
The opacity value of the color object, specified as a value from 0.0 to 1.0.
Return Value
An initialized color object. The color information represented by this object is in the device gray colorspace.
Discussion
Values below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted as 1.0.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIColor.hset
Sets the color of subsequent stroke and fill operations to the color that the receiver represents.
Discussion
If you subclass UIColor, you must implement this method in your subclass. Your custom implementation should modify both the stroke and fill color in the current graphics context by setting them both to the color represented by the receiver.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIColor.hsetFill
Sets the color of subsequent fill operations to the color that the receiver represents.
Discussion
If you subclass UIColor, you must implement this method in your subclass. Your custom implementation should modify the fill color in the current graphics context by setting it to the color represented by the receiver.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIColor.hsetStroke
Sets the color of subsequent stroke operations to the color that the receiver represents.
Discussion
If you subclass UIColor, you must implement this method in your subclass. Your custom implementation should modify the stroke color in the current graphics context by setting it to the color represented by the receiver.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIColor.h© 2012 Apple Inc. All Rights Reserved. (Last updated: 2012-12-13)