久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 Swift 中創建十六進制顏色字符串 UIColor 初

How to create a hex color string UIColor initializer in Swift?(如何在 Swift 中創建十六進制顏色字符串 UIColor 初始化程序?)
本文介紹了如何在 Swift 中創建十六進制顏色字符串 UIColor 初始化程序?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在使用此代碼從十六進制值創建 UIColor.它工作得很好.

I am using this code for create UIColor from hex value. Its working perfectly.

extension UIColor {
convenience init(red: Int, green: Int, blue: Int) {
   assert(red >= 0 && red <= 255, "Invalid red component")
   assert(green >= 0 && green <= 255, "Invalid green component")
   assert(blue >= 0 && blue <= 255, "Invalid blue component")

   self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)
}

convenience init(netHex:Int) {
   self.init(red:(netHex >> 16) & 0xff, green:(netHex >> 8) & 0xff, blue:netHex & 0xff)
}
}

用法:

var textColor = UIColor(netHex: 0xffffff)

此代碼非常適用于 Int 十六進制代碼.但它需要十六進制代碼 0xffffff 作為 Int 類型.我有來自網絡服務的十六進制代碼.它就像#ffffff"(字符串不是 Int).我可以將此字符串轉換為0xffffff".但我無法從0xffffff"(字符串)轉換為 0xffffff(整數).

This code works perfectly for Int hex code. But It needs hex code 0xffffff as Int type. I am having the hex code from web service. It will be like "#ffffff" (String not Int). I can convert this string like "0xffffff". But I can't convert from "0xffffff"(String) to 0xffffff (Int).

我需要這樣的東西

var textColor = UIColor(netHex: "0xffffff")

或者更好的是:

var textColor = UIColor(netHex: "#ffffff")

提前致謝.

推薦答案

Xcode 9 ? Swift 4 或更高版本

extension UIColor {
    convenience init?(hexaRGB: String, alpha: CGFloat = 1) {
        var chars = Array(hexaRGB.hasPrefix("#") ? hexaRGB.dropFirst() : hexaRGB[...])
        switch chars.count {
        case 3: chars = chars.flatMap { [$0, $0] }
        case 6: break
        default: return nil
        }
        self.init(red: .init(strtoul(String(chars[0...1]), nil, 16)) / 255,
                green: .init(strtoul(String(chars[2...3]), nil, 16)) / 255,
                 blue: .init(strtoul(String(chars[4...5]), nil, 16)) / 255,
                alpha: alpha)
    }

    convenience init?(hexaRGBA: String) {
        var chars = Array(hexaRGBA.hasPrefix("#") ? hexaRGBA.dropFirst() : hexaRGBA[...])
        switch chars.count {
        case 3: chars = chars.flatMap { [$0, $0] }; fallthrough
        case 6: chars.append(contentsOf: ["F","F"])
        case 8: break
        default: return nil
        }
        self.init(red: .init(strtoul(String(chars[0...1]), nil, 16)) / 255,
                green: .init(strtoul(String(chars[2...3]), nil, 16)) / 255,
                 blue: .init(strtoul(String(chars[4...5]), nil, 16)) / 255,
                alpha: .init(strtoul(String(chars[6...7]), nil, 16)) / 255)
    }

    convenience init?(hexaARGB: String) {
        var chars = Array(hexaARGB.hasPrefix("#") ? hexaARGB.dropFirst() : hexaARGB[...])
        switch chars.count {
        case 3: chars = chars.flatMap { [$0, $0] }; fallthrough
        case 6: chars.append(contentsOf: ["F","F"])
        case 8: break
        default: return nil
        }
        self.init(red: .init(strtoul(String(chars[2...3]), nil, 16)) / 255,
                green: .init(strtoul(String(chars[4...5]), nil, 16)) / 255,
                 blue: .init(strtoul(String(chars[6...7]), nil, 16)) / 255,
                alpha: .init(strtoul(String(chars[0...1]), nil, 16)) / 255)
    }
}

<小時>

if let textColor = UIColor(hexa: "00F") {
    print(textColor) // r 0.0 g 0.0 b 1.0 a 1.0
}

<小時>

if let textColor = UIColor(hexaRGB: "00F") {
    print(textColor) // r 0.0 g 0.0 b 1.0 a 1.0
}     


UIColor(hexaRGB: "#00F")                  // r 0.0 g 0.0 b 1.0 a 1.0
UIColor(hexaRGB: "#00F", alpha: 0.5)      // r 0.0 g 0.0 b 1.0 a 0.5

UIColor(hexaRGB: "#0000FF")               // r 0.0 g 0.0 b 1.0 a 1.0
UIColor(hexaRGB: "#0000FF", alpha: 0.5)   // r 0.0 g 0.0 b 1.0 a 0.5

UIColor(hexaRGBA: "#0000FFFF")            // r 0.0 g 0.0 b 1.0 a 1.0
UIColor(hexaRGBA: "#0000FF7F")            // r 0.0 g 0.0 b 1.0 a 0.498

UIColor(hexaARGB: "#FF0000FF")            // r 0.0 g 0.0 b 1.0 a 1.0
UIColor(hexaARGB: "#7F0000FF")            // r 0.0 g 0.0 b 1.0 a 0.498

這篇關于如何在 Swift 中創建十六進制顏色字符串 UIColor 初始化程序?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw stars using Quartz Core?(如何使用 Quartz Core 繪制星星?)
Why does giving addArcWithCenter a startAngle of 0 degrees make it start at 90 degrees?(為什么給 addArcWithCenter 一個 0 度的 startAngle 使它從 90 度開始?)
Find the CGPoint on a UIView rectangle intersected by a straight line at a given angle from the center point(在與中心點成給定角度的直線相交的 UIView 矩形上找到 CGPoint)
Which is the best way to estimate measure of photographed things?(哪種方法是估計拍攝物體尺寸的最佳方法?)
Convert Character to Int in Swift 2.0(在 Swift 2.0 中將字符轉換為 Int)
How to convert an Int to a Character in Swift(如何在 Swift 中將 Int 轉換為字符)
主站蜘蛛池模板: 成人黄色在线 | 国产一区 | 亚洲欧美日韩在线一区二区 | 综合自拍 | 亚洲精品日日夜夜 | 亚洲一区二区在线播放 | 亚洲视频中文字幕 | 免费看黄视频网站 | 91一区二区 | 蜜桃官网 | 在线一区视频 | 性色的免费视频 | 精品二区| 一区在线视频 | 亚洲一区二区黄 | 国产在线a | 日韩 欧美 二区 | 久久一区二区视频 | 在线视频日韩精品 | 在线观看成人小视频 | 久久精品在线 | 一区二区在线免费观看 | av一区二区在线观看 | 成人av在线网站 | 九九久久精品视频 | 成人免费共享视频 | 久久aⅴ乱码一区二区三区 亚洲国产成人精品久久久国产成人一区 | 国产亚洲www | 九九热在线免费视频 | 久久91精品国产 | 91精品久久久久久久久 | 国产精品免费看 | 欧美一级欧美一级在线播放 | 日韩三级电影一区二区 | 农村真人裸体丰满少妇毛片 | 四虎影音 | 国产精品不卡视频 | 久久久久久91香蕉国产 | 国产毛片视频 | 亚洲成人午夜电影 | 毛片免费看 |