Avatar ·

How to use hex color codes (HEX Color) in Swift and UIColor?

Hi! I'm new to Swift and iOS development, and I'm having a problem with colors. In web design, I'm used to using hex color codes, for example `#FF5733` for orange. But in Xcode, when working with `UIColor`, I only see initializers like `UIColor.red` or `UIColor(red: 1.0, green: 0.34, blue: 0.2, alpha: 1.0)`. This is very inconvenient — having to convert hex to decimal values every time. I tried writing a simple extension, but it doesn't work. Here's my code: ```swift extension UIColor { convenience init(hex: String) { let hexString = hex.trimmingCharacters(in: .whitespacesAndNewlines) let scanner = Scanner(string: hexString) var color: UInt64 = 0 scanner.scanHexInt64(&color) let r = CGFloat((color & 0xFF0000) >> 16) / 255.0 ```