[Swift 开发] iOS 颜色值#ffffff转UIColor
来源:互联网 发布:simcms二手车2.0 源码 编辑:程序博客网 时间:2024/06/16 18:13
写代码颜色值的时候会用到#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, green: green, blue: blue, 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, green: green, blue: blue, 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, green: green, blue: blue, 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(r * 255), Int(g * 255), Int(b * 255)) } } open override var description: String { return self.hexString(true) } open override var debugDescription: String { return self.hexString(true) }}
阅读全文
0 0
- [Swift 开发] iOS 颜色值#ffffff转UIColor
- IOS 颜色值#ffffff转UIColor
- iOS开发 十六进制颜色值转UIColor
- iOS使用#ffffff颜色
- Swift开发 UIColor分类Hex颜色转换
- iOS开发中16进制颜色(html颜色值)字符串转为UIColor
- ios开发之16进制颜色(html颜色值)字符串转为UIColor
- ios开发之16进制颜色(html颜色值)字符串转为UIColor
- iOS开发中16进制颜色(html颜色值)字符串转为UIColor
- iOS开发之16进制颜色(html颜色值)字符串转为UIColor
- 字符颜色值转UIColor
- 颜色值转化成UIColor IOS
- ios 16进制颜色转UIColor
- ios中十六进制颜色转UIColor
- ios中十六进制颜色转UIColor
- iOS-16进制颜色转UIColor
- IOS UIColor 自定义颜色
- IOS UIColor 自定义颜色
- 类相关的技术细节 -14
- ChinaVis 2017
- 图像DCT变换
- CSS3 box-sizing 属性 规定盒模型的宽高计算方式
- leetcode mysql
- [Swift 开发] iOS 颜色值#ffffff转UIColor
- windows下mysql 主从同步(由于XX主义,所以没有文字,不过十分明确表明这是转载!转载!转载!,非原创)
- Ubuntu16.4(64位)下gcc-linaro-arm-linux-gnueabihf交叉编译环境搭建
- 就这样学会怎么把图片上的文字提取出来的小方法
- 【JavaSE】常见的错误点总结1
- Android 在本地程序res/raw中读取数据操作
- Sublime 激活码
- SparseArray原理(阿里巴巴)
- 2017-11-3