美文网首页
iOS 颜色拓展之 "f8f8f8"转化为UIColor

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

作者: 如水的云如云的水 | 来源:发表于2018-04-21 14:33 被阅读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)

    }

}

相关文章

网友评论

      本文标题:iOS 颜色拓展之 "f8f8f8"转化为UIColor

      本文链接:https://www.haomeiwen.com/subject/jyjxlftx.html