How do I create a UIColor from RGBA?

前端 未结 5 1563
迷失自我
迷失自我 2020-12-23 21:39

I want to use NSAttributedString in my project, but when I\'m trying to set color, which isn\'t from the standard set (redColor, blackColor

5条回答
  •  误落风尘
    2020-12-23 22:02

    Since @Jaswanth Kumar asked, here's the Swift version from LSwift:

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

    Usage: let color = UIColor(rgb: 0x112233)

提交回复
热议问题