There are many ways you can create a UIColor
:
Swift
Using one of the predefined colors:
let redColor = UIColor.redColor()
let blueColor: UIColor = .blueColor()
// In Swift 3, the "Color()" suffix is removed:
let redColor = UIColor.red
let blueColor: UIColor = .blue
If the compiler already knows that the variable is an instance of UIColor
you can skip the type all together:
let view = UIView()
view.backgroundColor = .yellowColor()
Using the grayscale value and the alpha:
let grayscaleColor = UIColor(white: 0.5, alpha: 1.0)
Using hue, saturation, brightness and alpha:
let hsbColor = UIColor(
hue: 0.4,
saturation: 0.3,
brightness: 0.7,
alpha: 1.0
)
Using the RGBA values:
let rgbColor = UIColor(
red: 30.0 / 255,
green: 70.0 / 255,
blue: 200.0 / 255,
alpha: 1.0
)
Using a pattern image:
let patternColor = UIColor(patternImage: UIImage(named: "myImage")!)
Objective-C
Using one of the predefined colors:
UIColor *redColor = [UIColor redColor];
Using the grayscale value and the alpha:
UIColor *grayscaleColor = [UIColor colorWithWhite: 0.5 alpha: 1.0];
Using hue, saturation, brightness and alpha:
UIColor *hsbColor = [UIColor
colorWithHue: 0.4
saturation: 0.3
brightness: 0.7
alpha: 1.0
];
Using the RGBA values:
UIColor *rgbColor = [UIColor
colorWithRed: 30.0 / 255.0
green: 70.0 / 255.0
blue: 200.0 / 255.0
alpha: 1.0
];
Using a pattern image:
UIColor *pattenColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myImage.png"]];