-4

I am getting color value in response from webservice team like

"company_color_ses" = 373737;

How do I set this color to my navigation bar. Do I seprate string with 2 digits and set this color in R G B pattern. like R = 37 G = 37 B = 37. Or do anyone has any other solution?

Thanks

Chandni
  • 692
  • 1
  • 10
  • 25

3 Answers3

4

Use this extension of UIColor for hexcolor.

    extension UIColor {

        convenience init(hexString: String) {

            let hex = hexString.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
            var int = UInt32()
            Scanner(string: hex).scanHexInt32(&int)
            let a, r, g, b: UInt32
            switch hex.count {
            case 3: // RGB (12-bit)
                (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
            case 6: // RGB (24-bit)
                (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
            case 8: // ARGB (32-bit)
                (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
            default:
                (a, r, g, b) = (0, 0, 0, 0)
            }
            self.init(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)
        }

        var toHex: String? {
            return toHex()
        }

        // MARK: - From UIColor to String
        func toHex(alpha: Bool = false) -> String? {
            guard let components = cgColor.components, components.count >= 3 else {
                return nil
            }

            let r = Float(components[0])
            let g = Float(components[1])
            let b = Float(components[2])
            var a = Float(1.0)

            if components.count >= 4 {
                a = Float(components[3])
            }

            if alpha {
                return String(format: "%02lX%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255), lroundf(a * 255))
            } else {
                return String(format: "%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255))
            }
        }
}

use it like below:

navigationController?.navigationBar.backgroundColor = UIColor(hexString: 373737)
Khushbu
  • 2,342
  • 15
  • 18
  • The first method is a verbatim copy from https://stackoverflow.com/a/33397427/1187415, without attribution. – Martin R May 11 '18 at 06:33
  • @MartinR I am using it since long time. So, i have not searched for the link and directly put it here. – Khushbu May 11 '18 at 06:50
0

Change hex string to UIColor first, with method:

extension String {
    func colorFromHex() -> UIColor {
        let cString:String = self.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
        if ((cString.count) != 6) {
            return UIColor.gray
        }
        var rgbValue:UInt32 = 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)
        )
    }
}

And use it as:

    let colorString = "373737"
    navigationController?.navigationBar.barTintColor = colorString.colorFromHex()
Ankit Jayaswal
  • 5,549
  • 3
  • 16
  • 36
0

Use this simple UIColor Extension, to get color from Hex Code:-

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(rgb: Int) {
    self.init(
        red: (rgb >> 16) & 0xFF,
        green: (rgb >> 8) & 0xFF,
        blue: rgb & 0xFF
    )
}
}

Use of this:-

UIColor(rgb: 0xe3eaeb)
Manish Mahajan
  • 2,062
  • 1
  • 13
  • 19