iOS 颜色拓展之 "f8f8f8"转化为UIColor

2018-04-21  本文已影响0人  如水的云如云的水

给UIColor添加拓展方法,通过"f8f8f8"获取对应的UIColor值:

实际使用:let color = UIColor(hexColor: "f8f8f8")

importFoundation

import UIKit

extension UIColor {

   convenienceinit(hexColor:String) {       

        varred:UInt32=0, green:UInt32=0, blue:UInt32=0

       lethex = hexColorasNSString

        Scanner(string: hex.substring(with:NSRange(location:0, length:2))).scanHexInt32(&red)

        Scanner(string: hex.substring(with:NSRange(location:2, length:2))).scanHexInt32(&green)

        Scanner(string: hex.substring(with:NSRange(location:4, length:2))).scanHexInt32(&blue)

        self.init(red:CGFloat(red)/255.0, green:CGFloat(green)/255.0, blue:CGFloat(blue)/255.0, alpha:1.0)

    }

}

上一篇下一篇

猜你喜欢

热点阅读