最近我将代码库从Swift 3.2更新为Swift 4.2
所有突然被分配为颜色UIColor.lightGray
的UI元素在运行时都会产生黑色结果:
self.emptyTableView.messageLabel.textColor = UIColor.lightGray
你知道是什么原因造成的吗?
谢谢您的见解
编辑:
以下是我发现的UIColor扩展:
extension UIColor {
func colorWithRedValue(_ redValue: CGFloat, greenValue: CGFloat, blueValue: CGFloat, alpha: CGFloat) -> UIColor {
return UIColor(red: redValue/255.0, green: greenValue/255.0, blue: blueValue/255.0, alpha: alpha)
}
func facebookBlue() -> UIColor {
return UIColor(red:0.23, green:0.35, blue:0.60, alpha:1.0)
}
func twitterBlue() -> UIColor {
return UIColor(red:0.25, green:0.60, blue:1.00, alpha:1.0)
}
func goOutGreen() -> UIColor {
return UIColor(red:0.56, green:0.78, blue:0.25, alpha:1.0)
}
func goOutTransparent() -> UIColor {
return UIColor.white.withAlphaComponent(0.2)
}
func goOutClearGreen() -> UIColor {
return UIColor(red:0.00, green:0.91, blue:0.44, alpha:1.0)
}
func transparentBlack() -> UIColor {
return UIColor(red:0.00, green:0.00, blue:0.00, alpha:0.6)
}
func darkShade() -> UIColor {
return UIColor(red:0.13, green:0.13, blue:0.13, alpha:1.0)
}
func greenNormal() -> UIColor {
return UIColor(red:0.00, green:0.83, blue:0.15, alpha:1.0)
}
func goOutRed() -> UIColor {
return UIColor(red:0.77, green:0.05, blue:0.05, alpha:1.0)
}
func goOutBlue() -> UIColor {
return UIColor(red:0.29, green:0.56, blue:0.89, alpha:1.0)
}
func goOutGray() -> UIColor {
return UIColor(red:0.29, green:0.29, blue:0.29, alpha:1.0)
}
func adminBlack() -> UIColor {
return UIColor(red:0.09, green:0.09, blue:0.12, alpha:1.0)
}
func adminGray() -> UIColor {
return UIColor(red:0.902, green:0.902, blue:0.902, alpha:1)
}
func adminBackground() -> UIColor {
return UIColor(red:0.95, green:0.95, blue:0.96, alpha:1.0)
}
func adminPurple() -> UIColor {
return UIColor(red:0.36, green:0.33, blue:0.40, alpha:1.0)
}
func adminDarkPurple() -> UIColor {
return UIColor(red:0.265, green:0.24, blue:0.301, alpha:1)
}
func adminLightPurple() -> UIColor {
return UIColor(red:0.47, green:0.47, blue:0.47, alpha:1.0)
}
func eventDraft() -> UIColor {
return UIColor(red:0.91, green:0.21, blue:0.16, alpha:1.0)
}
func eventPending() -> UIColor {
return UIColor(red:0.08, green:0.54, blue:0.90, alpha:1.0)
}
convenience init(hexString: String) {
var cString: String = hexString.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
if (cString.hasPrefix("#")) {
cString = (cString as NSString).substring(from: 1)
}
if (cString.characters.count != 6) {
self.init(white: 0.5, alpha: 1.0)
} else {
let rString: String = (cString as NSString).substring(to: 2)
let gString = ((cString as NSString).substring(from: 2) as NSString).substring(to: 2)
let bString = ((cString as NSString).substring(from: 4) as NSString).substring(to: 2)
var r: CUnsignedInt = 0, g: CUnsignedInt = 0, b: CUnsignedInt = 0;
Scanner(string: rString).scanHexInt32(&r)
Scanner(string: gString).scanHexInt32(&g)
Scanner(string: bString).scanHexInt32(&b)
self.init(red: CGFloat(r) / CGFloat(255.0), green: CGFloat(g) / CGFloat(255.0), blue: CGFloat(b) / CGFloat(255.0), alpha: CGFloat(1))
}
}
}
和这个:
import Foundation
internal extension UIColor {
/// Hexadecimal representation of the UIColor.
/// For example, UIColor.blackColor() becomes "#000000".
var hex: String {
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
self.getRed(&red, green: &green, blue: &blue, alpha: nil)
let r = Int(255.0 * red)
let g = Int(255.0 * green)
let b = Int(255.0 * blue)
let str = String(format: "#%02x%02x%02x", r, g, b)
return str
}
}