写代码颜色值的时候会用到#ffffff 这样的格式,UIcolor不能直接识别,我们要转换一下。
在Swifttheme中看到了这个功能,在这里分享一下。
public enum UIcolorinputError : Error { case missingHashMarkAsPrefix,unabletoScanHexValue,mismatchedHexStringLength}extension UIcolor { /** The shorthand three-digit hexadecimal representation of color. #RGB defines to the color #RRGGBB. - parameter hex3: Three-digit hexadecimal value. - parameter Alpha: 0.0 - 1.0. The default is 1.0. */ public convenIEnce init(hex3: UInt16,Alpha: CGfloat = 1) { let divisor = CGfloat(15) let red = CGfloat((hex3 & 0xF00) >> 8) / divisor let green = CGfloat((hex3 & 0x0F0) >> 4) / divisor let blue = CGfloat( hex3 & 0x00F ) / divisor self.init(red: red,green: green,blue: blue,Alpha: Alpha) } /** The shorthand four-digit hexadecimal representation of color with Alpha. #RGBA defines to the color #RRGGBBAA. - parameter hex4: Four-digit hexadecimal value. */ public convenIEnce init(hex4: UInt16) { let divisor = CGfloat(15) let red = CGfloat((hex4 & 0xF000) >> 12) / divisor let green = CGfloat((hex4 & 0x0F00) >> 8) / divisor let blue = CGfloat((hex4 & 0x00F0) >> 4) / divisor let Alpha = CGfloat( hex4 & 0x000F ) / divisor self.init(red: red,Alpha: Alpha) } /** The six-digit hexadecimal representation of color of the form #RRGGBB. - parameter hex6: Six-digit hexadecimal value. */ public convenIEnce init(hex6: UInt32,Alpha: CGfloat = 1) { let divisor = CGfloat(255) let red = CGfloat((hex6 & 0xFF0000) >> 16) / divisor let green = CGfloat((hex6 & 0x00FF00) >> 8) / divisor let blue = CGfloat( hex6 & 0x0000FF ) / divisor self.init(red: red,Alpha: Alpha) } /** The six-digit hexadecimal representation of color with Alpha of the form #RRGGBBAA. - parameter hex8: Eight-digit hexadecimal value. */ public convenIEnce init(hex8: UInt32) { let divisor = CGfloat(255) let red = CGfloat((hex8 & 0xFF000000) >> 24) / divisor let green = CGfloat((hex8 & 0x00FF0000) >> 16) / divisor let blue = CGfloat((hex8 & 0x0000FF00) >> 8) / divisor let Alpha = CGfloat( hex8 & 0x000000FF ) / divisor self.init(red: red,Alpha: Alpha) } /** The rgba string representation of color with Alpha of the form #RRGGBBAA/#RRGGBB,throws error. - parameter rgba: String value. */ public convenIEnce init(rgba_throws rgba: String) throws { guard rgba.hasPrefix("#") else { throw UIcolorinputError.missingHashMarkAsPrefix } /*guard let hexString: String = rgba.substring(from: rgba.characters.index(rgba.startIndex,offsetBy: 1)),var hexValue: UInt32 = 0,Scanner(string: hexString).scanHexInt32(&hexValue) else { throw UIcolorinputError.unabletoScanHexValue }*/ let hexString: String = String(rgba[rgba.index(rgba.startIndex,offsetBy: 1)...]) var hexValue: UInt32 = 0 Scanner(string: hexString).scanHexInt32(&hexValue) guard hexString.characters.count == 3 || hexString.characters.count == 4 || hexString.characters.count == 6 || hexString.characters.count == 8 else { throw UIcolorinputError.mismatchedHexStringLength } switch (hexString.characters.count) { case 3: self.init(hex3: UInt16(hexValue)) case 4: self.init(hex4: UInt16(hexValue)) case 6: self.init(hex6: hexValue) default: self.init(hex8: hexValue) } } /** The rgba string representation of color with Alpha of the form #RRGGBBAA/#RRGGBB,fails to default color. - parameter rgba: String value. */ public convenIEnce init(rgba: String,defaultcolor: UIcolor = UIcolor.clear) { guard let color = try? UIcolor(rgba_throws: rgba) else { self.init(cgcolor: defaultcolor.cgcolor) return } self.init(cgcolor: color.cgcolor) } /** Hex string of a UIcolor instance. - parameter rgba: Whether the Alpha should be included. */ public func hexString(_ includeAlpha: Bool) -> String { var r: CGfloat = 0 var g: CGfloat = 0 var b: CGfloat = 0 var a: CGfloat = 0 self.getRed(&r,green: &g,blue: &b,Alpha: &a) if (includeAlpha) { return String(format: "#%02X%02X%02X%02X",Int(r * 255),Int(g * 255),Int(b * 255),Int(a * 255)) } else { return String(format: "#%02X%02X%02X",Int(b * 255)) } } open overrIDe var description: String { return self.hexString(true) } open overrIDe var deBUGDescription: String { return self.hexString(true) }}总结
以上是内存溢出为你收集整理的[Swift 开发] iOS 颜色值#ffffff转UIColor全部内容,希望文章能够帮你解决[Swift 开发] iOS 颜色值#ffffff转UIColor所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)