How can I use UIColorFromRGB in Swift?

后端 未结 20 603
一生所求
一生所求 2020-12-04 05:48

In Objective-C, we use this code to set RGB color codes for views:

#define UIColorFromRGB(rgbValue)        
[UIColor colorWithRed:((float)((rgbValue & 0x         


        
相关标签:
20条回答
  • 2020-12-04 06:02

    For Xcode 9, use UIColor with RGB values.

    shareBtn.backgroundColor = UIColor( red: CGFloat(92/255.0), green: CGFloat(203/255.0), blue: CGFloat(207/255.0), alpha: CGFloat(1.0) )
    

    Preview:

    See additional Apple documentation on UIColor.

    0 讨论(0)
  • 2020-12-04 06:02

    In Swift3, if you are starting with a color you have already chosen, you can get the RGB value online (http://imagecolorpicker.com) and use those values defined as a UIColor. This solution implements them as a background:

        @IBAction func blueBackground(_ sender: Any) {
            let blueColor = UIColor(red: CGFloat(160/255), green: CGFloat(183.0/255), blue: CGFloat(227.0/255), alpha: 1)
            view.backgroundColor = blueColor
    

    @Vadym mentioned this above in the comments and it is important to define the CGFloat with a single decimal point

    0 讨论(0)
  • 2020-12-04 06:03
    myLabel.backgroundColor = UIColor(red: 50.0/255, green: 150.0/255, blue: 65.0/255, alpha: 1.0)
    
    0 讨论(0)
  • 2020-12-04 06:04

    If you're starting from a string (not hex) this is a function that takes a hex string and returns a UIColor.
    (You can enter hex strings with either format: #ffffff or ffffff)

    Usage:

    var color1 = hexStringToUIColor("#d3d3d3")
    

    Swift 4:

    func hexStringToUIColor (hex:String) -> UIColor {
        var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
    
        if (cString.hasPrefix("#")) {
            cString.remove(at: cString.startIndex)
        }
    
        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)
        )
    }
    

    Swift 3:

    func hexStringToUIColor (hex:String) -> UIColor {
        var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
    
        if (cString.hasPrefix("#")) {
            cString.remove(at: cString.startIndex)
        }
    
        if ((cString.characters.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)
        )
    }
    

    Swift 2:

    func hexStringToUIColor (hex:String) -> UIColor {
        var cString:String = hex.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet() as NSCharacterSet).uppercaseString
    
        if (cString.hasPrefix("#")) {
          cString = cString.substringFromIndex(cString.startIndex.advancedBy(1))
        }
    
        if ((cString.characters.count) != 6) {
          return UIColor.grayColor()
        }
    
        var rgbValue:UInt32 = 0
        NSScanner(string: cString).scanHexInt(&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)
        )
    }
    



    Source: arshad/gist:de147c42d7b3063ef7bc

    0 讨论(0)
  • 2020-12-04 06:05

    I wanted to put

    cell.backgroundColor = UIColor.colorWithRed(125/255.0, green: 125/255.0, blue: 125/255.0, alpha: 1.0)  
    

    but that didn't work.

    So I used:
    For Swift

    cell.backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0)  
    

    So this is the workaround that I found.

    0 讨论(0)
  • 2020-12-04 06:05

    I really liked Nate Cook's answer but I wanted something a little more idiomatic. I believe this is a really good use case for a convenience initializer via a custom extension.

    // UIColorExtensions.swift
    import Foundation
    import UIKit
    
    extension UIColor {
        convenience init(rgb: UInt) {
            self.init(
                red: CGFloat((rgb & 0xFF0000) >> 16) / 255.0,
                green: CGFloat((rgb & 0x00FF00) >> 8) / 255.0,
                blue: CGFloat(rgb & 0x0000FF) / 255.0,
                alpha: CGFloat(1.0)
            )
        }
    }
    

    This can now be used like so:

    view.backgroundColor = UIColor(rgb: 0x209624)
    

    I would only recommend monkey patching UIKit classes like this in your own client code, not libraries.

    0 讨论(0)
提交回复
热议问题