본문 바로가기
💡 Today I Learned/🍯 개발 꿀팁

[꿀팁] Hexcode 변환 코드

by 솔비님 2024. 7. 4.
extension UIColor { 

	convenience init(hexCode: String, alpha: CGFloat = 1.0) { 
    	var hexFormatted: String = hexCode.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased() 
        if hexFormatted.hasPrefix("#") { 
        hexFormatted = String(hexFormatted.dropFirst()) 
        } 
        
        assert(hexFormatted.count == 6, "Invalid hex code used.") 
        
        var rgbValue: UInt64 = 0 
        Scanner(string: hexFormatted).scanHexInt64(&rgbValue) 
        
        self.init(red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0, 
        green: CGFloat((rgbValue & 0x00FF00) >> :선글라스: / 255.0, 
        blue: CGFloat(rgbValue & 0x0000FF) / 255.0, 
        alpha: alpha) 
    } 
}
segmentControl.backgroundColor = UIColor(hexCode: "A4BB94", alpha: 1.0)