Reputation: 1185
view.backgroundColor = UIColor.brownColor()
changes the background but it's limited to few colors, is there a way to use hex color codes?
Upvotes: 0
Views: 4602
Reputation: 644
use extensions
extension UIColor
{
class func myGreenColor() -> UIColor
{
return UIColor(red: 0.145, green: 0.608, blue: 0.141, alpha: 1.0)/*#259b24*/
}
}
this web converts hex to UIColor is objective-c , it should be easy for you to turn objective-c code into swift :P :D
Upvotes: 1
Reputation: 1605
There isn't a built in hex converter, but the one here works well for swift. Also has RGB converter if you prefer that.
How to use hex colour values in Swift, iOS
Code from accepted answer linked above:
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(netHex:Int) {
self.init(red:(netHex >> 16) & 0xff, green:(netHex >> 8) & 0xff, blue:netHex & 0xff)
}
}
Usage:
var color = UIColor(red: 0xFF, blue: 0xFF, green: 0xFF)
var color2 = UIColor(netHex:0xFFFFFF)
Upvotes: 1