建立 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"]];