【问题标题】:swift 3 convert string color to cgcolorswift 3将字符串颜色转换为cgcolor
【发布时间】:2017-04-07 12:03:27
【问题描述】:

我是 swift 和 iOS 的新手,有些事情我不知道该怎么办。

我有来自后端的颜色,我想将其转换为 CGColor

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

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

在将 CGColor 数组从 JSON 转换后,我应该在这里做什么来传递它。 我找不到正确的解决方案如何从简单的字符串中获取CGColor

【问题讨论】:

  • 各种步骤:看看如何从十六进制字符串创建 UIColor,然后UIColor 有一个属性CGColor(这是要使用的)。

标签: ios swift3 cgcolor


【解决方案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 }

【讨论】:

  • 谢谢@vadian。这正是我正在寻找的
【解决方案2】:

不是太难,但是从十六进制字符串创建 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)
        }
    }        
}

【讨论】:

    【解决方案3】:

    试试这个方法

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-11
      • 1970-01-01
      • 2017-08-11
      • 1970-01-01
      • 1970-01-01
      • 2017-03-07
      • 2018-02-26
      相关资源
      最近更新 更多