UIColor扩展—Swift
2016-01-05 13:44
405 查看
extension UIColor {
convenience init(bd_hexColor hexValue: String) {
var red: CGFloat = 0.0
var green: CGFloat = 0.0
var blue: CGFloat = 0.0
var alpha: CGFloat = 1.0
}
convenience init(bd_hexColor hexValue: String) {
var red: CGFloat = 0.0
var green: CGFloat = 0.0
var blue: CGFloat = 0.0
var alpha: CGFloat = 1.0
var hexColorString = hexValue; if !hexColorString.hasPrefix("#"){ hexColorString = "#"+hexValue; } let index = hexColorString.startIndex.advancedBy(1) let hex = hexColorString.substringFromIndex(index) let scanner = NSScanner(string: hex) var hexValue: CUnsignedLongLong = 0 if scanner.scanHexLongLong(&hexValue) { switch (hex.characters.count) { case 3: red = CGFloat((hexValue & 0xF00) >> 8) / 15.0 green = CGFloat((hexValue & 0x0F0) >> 4) / 15.0 blue = CGFloat(hexValue & 0x00F) / 15.0 case 4: red = CGFloat((hexValue & 0xF000) >> 12) / 15.0 green = CGFloat((hexValue & 0x0F00) >> 8) / 15.0 blue = CGFloat((hexValue & 0x00F0) >> 4) / 15.0 alpha = CGFloat(hexValue & 0x000F) / 15.0 case 6: red = CGFloat((hexValue & 0xFF0000) >> 16) / 255.0 green = CGFloat((hexValue & 0x00FF00) >> 8) / 255.0 blue = CGFloat(hexValue & 0x0000FF) / 255.0 case 8: red = CGFloat((hexValue & 0xFF000000) >> 24) / 255.0 green = CGFloat((hexValue & 0x00FF0000) >> 16) / 255.0 blue = CGFloat((hexValue & 0x0000FF00) >> 8) / 255.0 alpha = CGFloat(hexValue & 0x000000FF) / 255.0 default: BMLog("Invalid RGB string, number of characters after '#' should be either 3, 4, 6 or 8") } } else { BMLog("Scan hex error") } self.init(red:red, green:green, blue:blue, alpha:alpha) }
}
相关文章推荐
- swift之block
- swift学习
- 《swift2.0 官方教程中文版》 第2章-22协议
- Swift - 文本输入框(UITextField)的用法
- Swift2.1 枚举转换类型
- Swift中UIAlertController的用法
- swift tableView deleteRowsAtIndexPaths
- Swift-MJRefresh下拉刷新及上拉加载实践
- Swift中UITableView的用法(自定义UITableView和UITableViewCell)
- iOS动态启动界面(swift实现)
- iOS开发之基础控件(Swift版)
- Swift 实现单例模式Singleton pattern的三种方法
- swift学习 运算符 之 for循环
- iOS- Swift:指触即开,如何集成Touch ID指纹识别功能
- 用swift写的两种形式的AlartView
- Swift Tips笔记
- 《swift2.0 官方教程中文版》 第2章-21扩展
- swift安装cocoapod遇到的一些坑
- Swift 之类的继承与类的访问权限
- Swift - Struct 與 Class 的差異性