Skip to content

Instantly share code, notes, and snippets.

@azone
Last active March 23, 2016 10:21
Show Gist options
  • Save azone/b7e85367dd183ed4566a to your computer and use it in GitHub Desktop.
Save azone/b7e85367dd183ed4566a to your computer and use it in GitHub Desktop.
将16进制字符串或者整型转换成 UIColor
//
// UIColor+Utils.swift
//
// Created by Yozone Wang on 15/12/17.
// Copyright © 2015年 Yozone Wang. All rights reserved.
//
import UIKit
private let UIColorCache = NSCache()
private func intToColorComponent(intValue: UInt) -> CGFloat {
return CGFloat(intValue) / 255
}
extension UIColor {
/**
将 16 进制字符串转换成 UIColor,支持的格式有 RGB,RGBA,RRGGBB,RRGGBBAA,字符串可以有 # 或者 0x 前缀
- parameter hexString: 16 进制字符串
- returns: 转换后的 UIColor,如果不符合说明中的格式,则初始化失败,返回 nil
*/
convenience init?(hexString: String) {
var mutableString = hexString
if mutableString.hasPrefix("#") {
mutableString.removeAtIndex(mutableString.startIndex)
}
if mutableString.hasPrefix("0x") {
mutableString.removeRange(mutableString.startIndex..<mutableString.startIndex.advancedBy(2))
}
if let color = UIColorCache.objectForKey(mutableString) as? UIColor {
self.init(CGColor: color.CGColor)
return
}
guard let _ = mutableString.rangeOfString("^[0-9a-f]{3,8}$", options: [.RegularExpressionSearch, .CaseInsensitiveSearch]) else {
return nil
}
let redString: String, greenString: String, blueString: String, alphaString: String
switch mutableString.characters.count {
case 3:
redString = String(count: 2, repeatedValue: mutableString[mutableString.startIndex])
greenString = String(count: 2, repeatedValue: mutableString[mutableString.startIndex.advancedBy(1)])
blueString = String(count: 2, repeatedValue: mutableString[mutableString.startIndex.advancedBy(2)])
alphaString = "FF"
case 4:
redString = String(count: 2, repeatedValue: mutableString[mutableString.startIndex])
greenString = String(count: 2, repeatedValue: mutableString[mutableString.startIndex.advancedBy(1)])
blueString = String(count: 2, repeatedValue: mutableString[mutableString.startIndex.advancedBy(2)])
alphaString = String(count: 2, repeatedValue: mutableString[mutableString.startIndex.advancedBy(3)])
case 6:
redString = mutableString[mutableString.startIndex..<mutableString.startIndex.advancedBy(2)]
greenString = mutableString[mutableString.startIndex.advancedBy(2)..<mutableString.startIndex.advancedBy(4)]
blueString = mutableString[mutableString.startIndex.advancedBy(4)..<mutableString.startIndex.advancedBy(6)]
alphaString = "FF"
case 8:
redString = mutableString[mutableString.startIndex..<mutableString.startIndex.advancedBy(2)]
greenString = mutableString[mutableString.startIndex.advancedBy(2)..<mutableString.startIndex.advancedBy(4)]
blueString = mutableString[mutableString.startIndex.advancedBy(4)..<mutableString.startIndex.advancedBy(6)]
alphaString = mutableString[mutableString.startIndex.advancedBy(6)..<mutableString.startIndex.advancedBy(8)]
default:
return nil
}
let red = intToColorComponent(UInt(redString, radix: 16) ?? 0)
let green = intToColorComponent(UInt(greenString, radix: 16) ?? 0)
let blue = intToColorComponent(UInt(blueString, radix: 16) ?? 0)
let alpha = intToColorComponent(UInt(alphaString, radix: 16) ?? 0)
self.init(red: red, green: green, blue: blue, alpha: alpha)
UIColorCache.setObject(self, forKey: mutableString)
}
/**
将 32 位整型转换成 `UIColor`
- parameter intValue: UIColor 的 32 位整型数值,顺序为 RGBA,如果 intValue 小于等于 0xFFFFFF 则 alpha 值为 1
- returns: 转换后的 `UIColor`
*/
convenience init(intValue: UInt) {
if let color = UIColorCache.objectForKey(intValue) as? UIColor {
color
self.init(CGColor: color.CGColor)
return
}
let red: CGFloat
let green: CGFloat
let blue: CGFloat
let alpha: CGFloat
if intValue > 0xFFFFFF {
red = intToColorComponent((intValue >> 24) & 0xFF)
green = intToColorComponent((intValue >> 16) & 0xFF)
blue = intToColorComponent((intValue >> 8) & 0xFF)
alpha = CGFloat(intValue & 0xFF) / 255
} else {
red = intToColorComponent((intValue >> 16) & 0xFF)
green = intToColorComponent((intValue >> 8) & 0xFF)
blue = intToColorComponent(intValue & 0xFF)
alpha = 1
}
self.init(red: red, green: green, blue: blue, alpha: alpha)
UIColorCache.setObject(self, forKey: intValue)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment