swift 中 UIColor 的一个简单扩展 UIColor+Extension
2017-02-28 11:13
561 查看
完整代码
import Foundation
import UIKit
extension UIColor {
convenience init(r : CGFloat, g : CGFloat, b : CGFloat){
self.init(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: 1.0);
}
//简化RGB颜色写法
class func RGBA(_ r : UInt, g : UInt, b : UInt, a : CGFloat) -> UIColor {
let redFloat = CGFloat(r) / 255.0
let green = CGFloat(g) / 255.0
let blue = CGFloat(b) / 255.0
return UIColor(red: redFloat, green: green, blue: blue, alpha: a)
}
//随机色
class func randomColor() -> UIColor {
return UIColor(r: CGFloat(arc4random_uniform(256)), g: CGFloat(arc4random_uniform(256)), b: CGFloat(arc4random_uniform(256)));
}
//16进制颜色
class func colorWithHexString(_ hex: String, alpha : CGFloat = 1.0) -> UIColor {
var hex = hex
if hex.hasPrefix("#") {
hex = hex.substring(from: hex.characters.index(hex.startIndex, offsetBy: 1))
}
// Deal with 3 character Hex strings
if hex.characters.count == 3 {
let redHex = hex.substring(to: hex.characters.index(hex.startIndex, offsetBy: 1))
let greenHex = hex.substring(with: (hex.characters.index(hex.startIndex, offsetBy: 1) ..< hex.characters.index(hex.startIndex, offsetBy: 2)))
let blueHex = hex.substring(from: hex.characters.index(hex.startIndex, offsetBy: 2))
hex = redHex + redHex + greenHex + greenHex + blueHex + blueHex
}
let redHex = hex.substring(to: hex.characters.index(hex.startIndex, offsetBy: 2))
let greenHex = hex.substring(with: (hex.characters.index(hex.startIndex, offsetBy: 2) ..< hex.characters.index(hex.startIndex, offsetBy: 4)))
let blueHex = hex.substring(with: (hex.characters.index(hex.startIndex, offsetBy: 4) ..< hex.characters.index(hex.startIndex, offsetBy: 6)))
var redInt: CUnsignedInt = 0
var greenInt: CUnsignedInt = 0
var blueInt: CUnsignedInt = 0
Scanner(string: redHex).scanHexInt32(&redInt)
Scanner(string: greenHex).scanHexInt32(&greenInt)
Scanner(string: blueHex).scanHexInt32(&blueInt)
return UIColor(red: CGFloat(redInt) / 255.0, green: CGFloat(greenInt) / 255.0, blue: CGFloat(blueInt) / 255.0, alpha: CGFloat(alpha))
}
}
使用举例
RGBA
tableView.backgroundColor = UIColor.RGBA(53, g: 46, b: 54, a: 1)
十六进制颜色
self.tabBarController?.tabBar.barTintColor = UIColor.colorWithHexString("#252122")
import Foundation
import UIKit
extension UIColor {
convenience init(r : CGFloat, g : CGFloat, b : CGFloat){
self.init(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: 1.0);
}
//简化RGB颜色写法
class func RGBA(_ r : UInt, g : UInt, b : UInt, a : CGFloat) -> UIColor {
let redFloat = CGFloat(r) / 255.0
let green = CGFloat(g) / 255.0
let blue = CGFloat(b) / 255.0
return UIColor(red: redFloat, green: green, blue: blue, alpha: a)
}
//随机色
class func randomColor() -> UIColor {
return UIColor(r: CGFloat(arc4random_uniform(256)), g: CGFloat(arc4random_uniform(256)), b: CGFloat(arc4random_uniform(256)));
}
//16进制颜色
class func colorWithHexString(_ hex: String, alpha : CGFloat = 1.0) -> UIColor {
var hex = hex
if hex.hasPrefix("#") {
hex = hex.substring(from: hex.characters.index(hex.startIndex, offsetBy: 1))
}
// Deal with 3 character Hex strings
if hex.characters.count == 3 {
let redHex = hex.substring(to: hex.characters.index(hex.startIndex, offsetBy: 1))
let greenHex = hex.substring(with: (hex.characters.index(hex.startIndex, offsetBy: 1) ..< hex.characters.index(hex.startIndex, offsetBy: 2)))
let blueHex = hex.substring(from: hex.characters.index(hex.startIndex, offsetBy: 2))
hex = redHex + redHex + greenHex + greenHex + blueHex + blueHex
}
let redHex = hex.substring(to: hex.characters.index(hex.startIndex, offsetBy: 2))
let greenHex = hex.substring(with: (hex.characters.index(hex.startIndex, offsetBy: 2) ..< hex.characters.index(hex.startIndex, offsetBy: 4)))
let blueHex = hex.substring(with: (hex.characters.index(hex.startIndex, offsetBy: 4) ..< hex.characters.index(hex.startIndex, offsetBy: 6)))
var redInt: CUnsignedInt = 0
var greenInt: CUnsignedInt = 0
var blueInt: CUnsignedInt = 0
Scanner(string: redHex).scanHexInt32(&redInt)
Scanner(string: greenHex).scanHexInt32(&greenInt)
Scanner(string: blueHex).scanHexInt32(&blueInt)
return UIColor(red: CGFloat(redInt) / 255.0, green: CGFloat(greenInt) / 255.0, blue: CGFloat(blueInt) / 255.0, alpha: CGFloat(alpha))
}
}
使用举例
RGBA
tableView.backgroundColor = UIColor.RGBA(53, g: 46, b: 54, a: 1)
十六进制颜色
self.tabBarController?.tabBar.barTintColor = UIColor.colorWithHexString("#252122")
相关文章推荐
- Swift UIColor Extension (扩展)
- 创建一个扩展DLL——CExtensionDLL项目
- 一个简单的Dreamweaver 扩展源文件(mxi)
- (Python编程)一个简单的C扩展模块
- PHP扩展开发:一个简单的例子
- SNMP--How to develop a SNMP extension agent DLL(如何开发一个SNMP扩展代理DLL)
- 一个对于Array的简单扩展
- Swift扩展(Extension)
- 自己动手写一个简单的Windows shell扩展程序
- Firefox扩展开发初级教程——开发一个简单的扩展
- Swift 协议(protocol)和扩展(extension)
- 最简单直接继承、扩展Object,或者克隆一个Object到能创建多个互不干扰实例的方法
- 编写一个简单的php扩展
- 一个简单的tcl的dll扩展事例
- 自己动手写一个简单的Windows shell扩展程序
- 对一个简单的 JDBC 包装器的扩展及应用
- Apache模块开发/用C语言扩展apache(3:一个非常简单的apache module)
- 最简单直接继承、扩展Object,或者克隆一个Object到能创建多个互不干扰实例的方法
- Libgdx Developer's Guide(Libgdx开发者手册)-9(一个简单的游戏2--扩展示例游戏)
- Swift学习这二十二:扩展(extension)