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

给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)

扫描二维码关注公众号,回复: 180960 查看本文章


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

    }

}


猜你喜欢

转载自blog.csdn.net/flyingfirefish/article/details/80029377