swift之颜⾊、16进制颜⾊转换成RGB颜⾊
//16进制字符串转成UIColor
import UIKit
class LYBColorExtention: NSObject {
}
//⽅法⼀:使⽤:
//v.backgroundColor="#eeeeee".toUIColor()
extension String{
/// 将⼗六进制颜⾊转UIColor
///⽅法⼀  - Returns: UIColor
public func toUIColor() -> UIColor {
/
/处理数值
var cString = self.uppercased().trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
let length = (cString as NSString).length
//错误处理
if (length < 6 || length > 7 || (!cString.hasPrefix("#") && length == 7)){
return UIColor.white
}
if cString.hasPrefix("#"){
cString = (cString as NSString).substring(from: 1)
}
//字符chuan截取
var range = NSRange()
range.location = 0
range.length = 2
let rString = (cString as NSString).substring(with: range)
range.location = 2
let gString = (cString as NSString).substring(with: range)
cstring转为int
range.location = 4
let bString = (cString as NSString).substring(with: range)
//存储转换后的数值
var r:UInt32 = 0,g:UInt32 = 0,b:UInt32 = 0
//进⾏转换
Scanner(string: rString).scanHexInt32(&r)
Scanner(string: gString).scanHexInt32(&g)
Scanner(string: bString).scanHexInt32(&b)
//根据颜⾊值创建UIColor
return UIColor(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: 1.0)
}
//⽅法⼆;"".color
var color: UIColor {
let hex = trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
if #available(iOS 13, *) {
guard let int = Scanner(string: hex).scanInt32(representation: .hexadecimal) else { return #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0) }
let a, r, g, b: Int32
unt {
case 3:    (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)  // RGB (12-bit)
case 6:    (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)                    // RGB (24-bit)
case 6:    (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)                    // RGB (24-bit)
case 8:    (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)      // ARGB (32-bit)
default:    (a, r, g, b) = (255, 0, 0, 0)
}
return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(a) / 255.0)
} else {
var int = UInt32()
Scanner(string: hex).scanHexInt32(&int)
let a, r, g, b: UInt32
unt {
case 3:    (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)  // RGB (12-bit)
case 6:    (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)                    // RGB (24-bit)
case 8:    (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)      // ARGB (32-bit)
default:    (a, r, g, b) = (255, 0, 0, 0)
}
return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(a) / 255.0)        }
}
}
//v.backgroundColor=UIColor.init(hexString: "#88eeee")
extension UIColor{
//    //⽅法三
//    convenience init(hexString:String){
//        //处理数值
//        var cString = hexString.uppercased().trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
//
//        let length = (cString as NSString).length
//        //错误处理
//        if (length < 6 || length > 7 || (!cString.hasPrefix("#") && length == 7)){
//            //返回whiteColor
//            self.init(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
//            return
//        }
//
//        if cString.hasPrefix("#"){
//            cString = (cString as NSString).substring(from: 1)
//        }
//
//        //字符chuan截取
//        var range = NSRange()
//        range.location = 0
/
/        range.length = 2
//
//        let rString = (cString as NSString).substring(with: range)
//
//        range.location = 2
//        let gString = (cString as NSString).substring(with: range)
//
//        range.location = 4
//        let bString = (cString as NSString).substring(with: range)
//
//        //存储转换后的数值
/
/        var r:UInt32 = 0,g:UInt32 = 0,b:UInt32 = 0
//        //进⾏转换
//        Scanner(string: rString).scanHexInt32(&r)
//        Scanner(string: gString).scanHexInt32(&g)
//        Scanner(string: bString).scanHexInt32(&b)
//        //根据颜⾊值创建UIColor
//        self.init(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: 1.0)
//    }
//    }
//⽅法四
class func hexColor(hex:String) -> UIColor {
var cString:String = immingCharacters(in: .whitespacesAndNewlines).uppercased()
if (cString.hasPrefix("#")) {
}
if ((unt) != 6) {
ay
}
var rgbValue:UInt64 = 0
Scanner(string: cString).scanHexInt64(&rgbValue)
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。