美文网首页
UIColor 扩展

UIColor 扩展

作者: 白色天空729 | 来源:发表于2018-05-21 23:00 被阅读18次
    extension UIColor {
        
        //十六进制颜色值转换
        class func colorWithHex(_rgbValue:Int,_alpha:CGFloat = 1.0)-> UIColor {
        return UIColor(red: ((CGFloat)((_rgbValue & 0xFF0000) >> 16)) / 255.0,green: ((CGFloat)((_rgbValue & 0xFF00) >> 8)) / 255.0,blue: ((CGFloat)(_rgbValue & 0xFF)) / 255.0,alpha: _alpha)
        }
    
        /// 快速构建rgb颜色
        ///
        /// - Parameters:
        ///   - r: r
        ///   - g: g
        ///   - b: b
        /// - Returns: 返回rgb颜色对象,alpha默认1
        class func colorWithRGB(_ r: CGFloat, g: CGFloat, b: CGFloat) -> UIColor {
            return UIColor(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: 1)
        }
    
        /// 生成随机颜色
        ///
        /// - Returns: 返回随机色
        class func randomColor() -> UIColor {
            let r = CGFloat(arc4random_uniform(256))
            let g = CGFloat(arc4random_uniform(256))
            let b = CGFloat(arc4random_uniform(256))
            return UIColor.colorWithRGB(r, g: g, b: b)
        }
        
        
        /// 16进制转UIColor
        ///
        /// - Parameter hex: 16进制
        /// - Returns: UIColor
        class func colorWithHexString(_ hex:String) -> UIColor {
            var cString:String = hex.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines).uppercased()
            
            if (cString.hasPrefix("#")) {
                cString = (cString as NSString).substring(from: 1)
            }
            
            if (cString.count != 6) {
                return UIColor.gray
            }
            
            let rString = (cString as NSString).substring(to: 2)
            let gString = ((cString as NSString).substring(from: 2) as NSString).substring(to: 2)
            let bString = ((cString as NSString).substring(from: 4) as NSString).substring(to: 2)
            
            var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;
            Scanner(string: rString).scanHexInt32(&r)
            Scanner(string: gString).scanHexInt32(&g)
            Scanner(string: bString).scanHexInt32(&b)
            
            return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(1))
        }
        
        /// 返回16进制颜色
        class func colorWithHexString(_ hex: String, alpha: CGFloat) -> UIColor {
            var cString:String = hex.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines).uppercased()
            
            if (cString.hasPrefix("#")) {
                cString = (cString as NSString).substring(from: 1)
            }
            
            if (cString.count != 6) {
                return UIColor.gray
            }
            
            let rString = (cString as NSString).substring(to: 2)
            let gString = ((cString as NSString).substring(from: 2) as NSString).substring(to: 2)
            let bString = ((cString as NSString).substring(from: 4) as NSString).substring(to: 2)
            
            var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;
            Scanner(string: rString).scanHexInt32(&r)
            Scanner(string: gString).scanHexInt32(&g)
            Scanner(string: bString).scanHexInt32(&b)
            
            return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: alpha)
        }
    }
    
    

    相关文章

      网友评论

          本文标题:UIColor 扩展

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