How can I use UIColorFromRGB in Swift?

后端 未结 20 606
一生所求
一生所求 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:05

    adding a swift 3 option:

    cell.layer.borderColor = UIColor (red: 192.0/255.0, green: 192.0/255.0, blue: 197/255.0, alpha: 1.0).cgColor
    
    0 讨论(0)
  • 2020-12-04 06:05

    That can be simply done by using this initialization

    view.backgroundColor = UIColor(hex: "067AB5")
    
    0 讨论(0)
  • 2020-12-04 06:06

    I have used the following in swift.

    let appRedColor = UIColor(red: 200.0/255.0, green: 16.0/255.0, blue: 46.0/255.0, alpha: 1.0)
    let appSilverColor = UIColor(red: 236.0/255.0, green: 236.0/255.0, blue: 236.0/255.0, alpha: 1.0)
    let appWhiteColor = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0)
    let appNavyColor = UIColor(red: 19.0/255.0, green: 41.0/255.0, blue: 75.0/255.0, alpha: 1.0)
    
    0 讨论(0)
  • 2020-12-04 06:12

    This is a nice extension for UIColor. You can use enum values(hex, string) and direct string values when you creating UIColor objects.

    The extension we deserve https://github.com/ioramashvili/UsefulExtensions/blob/master/Extensions.playground/Pages/UIColor.xcplaygroundpage/Contents.swift

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

    solution for argb format:

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

    usage:

    var clearColor: UIColor = UIColor.init(argb: 0x00000000)
    var redColor: UIColor = UIColor.init(argb: 0xFFFF0000)
    
    0 讨论(0)
  • 2020-12-04 06:15

    UIColorFromRGB in Swift 4

    button.layer.backgroundColor = UIColor(red: 112.0/255, green: 86.0/255, blue: 164.0/255, alpha: 1.0).cgColor
    
    0 讨论(0)
提交回复
热议问题