swift 中 UIColor 的一个简单扩展 UIColor+Extension

swift 中 UIColor 的一个简单扩展 UIColor+Extension,第1张

概述完整代码 import Foundationimport UIKitextension 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); }

完整代码

import Foundationimport UIKitextension 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 的一个简单扩展 UIColor+Extension全部内容,希望文章能够帮你解决swift 中 UIColor 的一个简单扩展 UIColor+Extension所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1066164.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存