如何使用Swift将UIColor转换为字符串并将字符串转换为UIColor? [英] How to convert UIColor to string and string to UIColor using swift?

查看:101
本文介绍了如何使用Swift将UIColor转换为字符串并将字符串转换为UIColor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有UIColor属性的对象:

I have an object with a UIColor property:

class Beer: NSObject {
  var color: UIColor?
  ...
}

我将其保存到数据库中,因此需要将该属性设置为有效的JSON类型,因此我正在考虑将其转换为字符串.如何将其转换为要存储的字符串,然后在使用该字符串创建UIColor时进行加载?

I'm saving this to a DB, so I need to make this property into a valid JSON type, so I'm thinking of converting it to a string. How can I convert into a string to store, and then when loading using that string to create the UIColor?

推荐答案

我已经为两次转换提供了一些示例,但是您仍然可以找到许多用于转换的代码

I have put some sample for both conversion, still you can find many code for the conversion

要从UIColor转换为十六进制字符串,可以使用以下代码:

For the conversion from UIColor to hex string you can use the following code:

extension UIColor {
    var rgbComponents:(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
        var r:CGFloat = 0
        var g:CGFloat = 0
        var b:CGFloat = 0
        var a:CGFloat = 0
        if getRed(&r, green: &g, blue: &b, alpha: &a) {
            return (r,g,b,a)
        }
        return (0,0,0,0)
    }
    // hue, saturation, brightness and alpha components from UIColor**
    var hsbComponents:(hue: CGFloat, saturation: CGFloat, brightness: CGFloat, alpha: CGFloat) {
        var hue:CGFloat = 0
        var saturation:CGFloat = 0
        var brightness:CGFloat = 0
        var alpha:CGFloat = 0
        if getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha){
            return (hue,saturation,brightness,alpha)
        }
        return (0,0,0,0)
    }
    var htmlRGBColor:String {
        return String(format: "#%02x%02x%02x", Int(rgbComponents.red * 255), Int(rgbComponents.green * 255),Int(rgbComponents.blue * 255))
    }
    var htmlRGBaColor:String {
        return String(format: "#%02x%02x%02x%02x", Int(rgbComponents.red * 255), Int(rgbComponents.green * 255),Int(rgbComponents.blue * 255),Int(rgbComponents.alpha * 255) )
    }
}

样品使用:

let myColorBlack = UIColor.blackColor().webColor         //#000000ff
let myLghtGrayColor = UIColor.lightGrayColor().webColor  //#aaaaaaff
let myDarkGrayColor = UIColor.darkGrayColor().webColor 

有关更多信息,您可以检查: https://stackoverflow.com/a/28697136/4557505
https://gist.github.com/yannickl/16f0ed38f0698d9a8ae7

For more info you can check: https://stackoverflow.com/a/28697136/4557505
https://gist.github.com/yannickl/16f0ed38f0698d9a8ae7

您可以将此字符串存储在数据库中,并在需要时检索它

You can store this string inside the db and retrieve it when you need it

从HexString到UIColor

From HexString to UIColor

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

        if hexString.hasPrefix("#") {
            let start = hexString.startIndex.advancedBy(1)
            let hexColor = hexString.substringFromIndex(start)

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

                if scanner.scanHexLongLong(&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
    }
}

用法:UIColor(hexString:#ffe700ff")

Usage:UIColor(hexString: "#ffe700ff")

参考:https://www.hackingwithswift.com/example-code/uicolor/how-to-convert-a-hex-color-to-a-uicolor
https://github.com/yeahdongcn/UIColor-Hex-Swift
https://gist.github.com/arshad/de147c42d7b3063ef7bc

这篇关于如何使用Swift将UIColor转换为字符串并将字符串转换为UIColor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆