2016-11-23 83 views
2

我是swift和iOS上的新成員,並且有些事情我不知道該怎麼辦。swift 3將字符串顏色轉換爲cgcolor

我有顏色從後端快到了,我想將其轉換爲CGColor

"colors": [ 
     "6909A1", 
     "7552D1", 
     "59B0DC", 
     "62E3CC" 
     ], 

func drawGradient(colors: [CGColor]) { 
     addLayer(colors: colors) 
    } 

我應該怎麼做這裏從JSON轉換之後通過CGColor陣列。 我找不到合適的解決方案如何從簡單字符串獲得CGColor字符串

+2

各個步驟:看看如何從十六進制字符串創建的UIColor,然後'UIColor'有一個屬性'CGColor'(這是使用的)。 – Larme

回答

1

UInt擁有一家便利初始化爲十六進制字符串轉換爲它的十六進制值

func color(from hexString : String) -> CGColor 
{ 
    if let rgbValue = UInt(hexString, radix: 16) { 
    let red = CGFloat((rgbValue >> 16) & 0xff)/255 
    let green = CGFloat((rgbValue >> 8) & 0xff)/255 
    let blue = CGFloat((rgbValue  ) & 0xff)/255 
    return UIColor(red: red, green: green, blue: blue, alpha: 1.0).cgColor 
    } else { 
    return UIColor.black.cgColor 
    } 
} 

查閱映射字符串數組到顏色陣列

let hexColors = ["6909A1", "7552D1", "59B0DC", "62E3CC"] 

let gradientColors = hexColors.map { color(from:$0) } 

或可選擇地在UIColor擴展

extension UIColor { 

    convenience init(hexString : String) 
    { 
     if let rgbValue = UInt(hexString, radix: 16) { 
      let red = CGFloat((rgbValue >> 16) & 0xff)/255 
      let green = CGFloat((rgbValue >> 8) & 0xff)/255 
      let blue = CGFloat((rgbValue  ) & 0xff)/255 
      self.init(red: red, green: green, blue: blue, alpha: 1.0) 
     } else { 
      self.init(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0) 
     } 
    } 
} 

let hexColors = ["6909A1", "7552D1", "59B0DC", "62E3CC"] 

let gradientColors = hexColors.map { UIColor(hexString:$0).cgColor } 
+0

謝謝@vadian。這正是我正在尋找的 – pmb

1

不是太難,但是從十六進制字符串創建UIColor/CGColor對象的能力並未嵌入到Swift中。但是,您可以只把這個延伸到你的項目,將其添加:

extension UIColor { 
    public convenience init?(hexString: String) { 
     let r, g, b, a: CGFloat 

     if hexString.hasPrefix("#") { 
      let start = hexString.index(hexString.startIndex, offsetBy: 1) 
      let hexColor = hexString.substring(from: start) 

      if hexColor.characters.count == 8 { 
       let scanner = Scanner(string: hexColor) 
       var hexNumber: UInt64 = 0 

       if scanner.scanHexInt64(&hexNumber) { 
        r = CGFloat((hexNumber & 0xff000000) >> 24)/255 
        g = CGFloat((hexNumber & 0x00ff0000) >> 16)/255 
        b = CGFloat((hexNumber & 0x0000ff00) >> 8)/255 
        a = CGFloat(hexNumber & 0x000000ff)/255 

        self.init(red: r, green: g, blue: b, alpha: a) 
        return 
       } 
      } 
     } 

     return nil 
    } 
} 

那麼你可以做這樣的事情是十六進制顏色數組轉換成CGColor陣列:

func convertColours() { 
    let stringColours = ["6909A1", 
         "7552D1", 
         "59B0DC", 
         "62E3CC"] 
    var colours = [CGColor]() 

    for stringColour in stringColours { 
     if let colour = UIColor(hexString: stringColour) { 
      colours.append(colour.cgColor) 
     } 
    }   
} 
0

嘗試這樣

extension UIColor { 
    convenience init(red: Int, green: Int, blue: Int) { 
     self.init(red: CGFloat(red)/255.0, green: CGFloat(green)/255.0, blue: CGFloat(blue)/255.0, alpha: 1.0) 
    } 

    func color(with hex: Int) -> CGColor { 
     let color = UIColor(red:(hex >> 16) & 0xff, green:(hex >> 8) & 0xff, blue:hex & 0xff) 
     return color.cgColor 
    } 
} 
UIColor().color(with: Int(stringColours[0], radix: 16)!) 
UIColor().color(with: 0x333333) 
相關問題