Kru*_*nal 8 hex uicolor swift ios13
scanHexInt32iOS 13(Swift 5+)中有哪些替代品?
extension UIColor {
//--------------------------------------------
class func hexColor(hex:String) -> UIColor {
var cString:String = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()
if (cString.hasPrefix("#")) {
cString = String(cString[cString.index(cString.startIndex, offsetBy: 1)...])
}
if (cString.count != 6) {
return UIColor.gray
}
var rgbValue:UInt32 = 0
// warning in this line - 'scanHexInt32' was deprecated in iOS 13.0
Scanner(string: cString).scanHexInt32(&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)
)
}
}
Run Code Online (Sandbox Code Playgroud)
参考:快照
还有另一种实例方法可用
scanInt32(representation:)
Run Code Online (Sandbox Code Playgroud)
宣言:
func scanInt32(representation: Scanner.NumberRepresentation = .decimal) -> Int32?
Run Code Online (Sandbox Code Playgroud)
在这里你必须传递 enum .hexadecimal。
我希望它会返回相同的结果。结果将是可选的。
看起来苹果公司正在从其64位操作系统中逐步淘汰Int32。尝试将您的代码转换为使用Int64。
@available(iOS, introduced: 2.0, deprecated: 13.0)
open func scanHexInt32(_ result: UnsafeMutablePointer<UInt32>?) -> Bool // Optionally prefixed with "0x" or "0X"
@available(iOS 2.0, *)
open func scanHexInt64(_ result: UnsafeMutablePointer<UInt64>?) -> Bool // Optionally prefixed with "0x" or "0X"
Run Code Online (Sandbox Code Playgroud)
更新为使用UInt64和scanHexInt64:
convenience init(hex: String, alpha: CGFloat = 1.0) {
var hexFormatted = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()
if hexFormatted.hasPrefix("#") {
hexFormatted = String(hexFormatted.dropFirst())
}
assert(hexFormatted.count == 6, "Invalid hex code used.")
var rgbValue: UInt64 = 0
Scanner(string: hexFormatted).scanHexInt64(&rgbValue)
self.init(red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: alpha)
}
Run Code Online (Sandbox Code Playgroud)
尝试这个扩展 swiftui :
extension Color {
init(hex: String) {
let scanner = Scanner(string: hex)
scanner.currentIndex = scanner.string.startIndex
var rgbValue: UInt64 = 0
scanner.scanHexInt64(&rgbValue)
let r = (rgbValue & 0xff0000) >> 16
let g = (rgbValue & 0xff00) >> 8
let b = rgbValue & 0xff
self.init(red: Double(r) / 0xff, green: Double(g) / 0xff, blue: Double(b) / 0xff)
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用这个 uicolor 扩展..请找到下面的代码
extension UIColor {
class func hexColor(hex:String) -> UIColor {
var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
if (cString.hasPrefix("#")) {
cString.remove(at: cString.startIndex)
}
if ((cString.count) != 6) {
return UIColor.gray
}
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)
)
} }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1190 次 |
| 最近记录: |