创建 UIColor
你可以通过多种方式创建 UIColor
:
迅速
-
使用其中一种预定义颜色:
let redColor = UIColor.redColor() let blueColor: UIColor = .blueColor() // In Swift 3, the "Color()" suffix is removed: let redColor = UIColor.red let blueColor: UIColor = .blue
如果编译器已经知道变量是
UIColor
的一个实例,你可以一起跳过所有类型:let view = UIView() view.backgroundColor = .yellowColor()
-
使用灰度值和 alpha:
let grayscaleColor = UIColor(white: 0.5, alpha: 1.0)
-
使用色调,饱和度,亮度和 alpha:
let hsbColor = UIColor( hue: 0.4, saturation: 0.3, brightness: 0.7, alpha: 1.0 )
-
使用 RGBA 值:
let rgbColor = UIColor( red: 30.0 / 255, green: 70.0 / 255, blue: 200.0 / 255, alpha: 1.0 )
-
使用图案图像:
let patternColor = UIColor(patternImage: UIImage(named: "myImage")!)
Objective-C
-
使用其中一种预定义颜色:
UIColor *redColor = [UIColor redColor];
-
使用灰度值和 alpha:
UIColor *grayscaleColor = [UIColor colorWithWhite: 0.5 alpha: 1.0];
-
使用色调,饱和度,亮度和 alpha:
UIColor *hsbColor = [UIColor colorWithHue: 0.4 saturation: 0.3 brightness: 0.7 alpha: 1.0 ];
-
使用 RGBA 值:
UIColor *rgbColor = [UIColor colorWithRed: 30.0 / 255.0 green: 70.0 / 255.0 blue: 200.0 / 255.0 alpha: 1.0 ];
-
使用图案图像:
UIColor *pattenColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myImage.png"]];