UIcolor大小事

運用色碼表指定顏色

方法1:

loadingView.backgroundColor = UIColorFromHex(rgbValue: 0x444444, alpha: 0.7)
func UIColorFromHex(rgbValue:UInt32, alpha:Double=1.0)->UIColor {
  let red = CGFloat((rgbValue & 0xFF0000) >> 16)/256.0
  let green = CGFloat((rgbValue & 0xFF00) >> 8)/256.0
  let blue = CGFloat(rgbValue & 0xFF)/256.0
  return UIColor(red:red, green:green, blue:blue,  alpha:CGFloat(alpha))
}

方法2 :

viewBackgroundLoading.backgroundColor = hexStringToUIColor(hex: “0x444444”)

func hexStringToUIColor (hex:String) -> UIColor {
var cString:String = hex.trimmingCharacters(in:.whitespacesAndNewlines).uppercased()
if (cString.hasPrefix(“#”)) {
     cString.remove(at: cString.startIndex)
}
if ((cString.characters.count) != 6) {
return UIColor.gray
}
var rgbValue:UInt32 = 0
Scanner(string: cString).scanHexInt32(&rgbValue)
return UIColor(red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,blue: CGFloat(rgbValue & 0x0000FF) / 255.0,alpha: CGFloat(1.0)
)

}