참고 : https://crunchybagel.com/working-with-hex-colors-in-swift-3/


RGB 헥사 값을 UIColor로 변환 사용하는 예

extension UIColor {

    convenience init(hex: String) {

        let scanner = Scanner(string: hex)

        scanner.scanLocation = 0

        var rgbValue: UInt64 = 0        

        scanner.scanHexInt64(&rgbValue)

        

        let r = (rgbValue & 0xff0000) >> 16

        let g = (rgbValue & 0xff00) >> 8

        let b = rgbValue & 0xff

        

        self.init(

            red: CGFloat(r) / 0xff,

            green: CGFloat(g) / 0xff,

            blue: CGFloat(b) / 0xff, alpha: 1

        )

    }

}

let color = UIColor(hex: "ff0000") 



유용한 정보였길 바란다.

'Swift > ' 카테고리의 다른 글

유용하게 쓰이는 랜덤 함수  (0) 2017.10.14
UserDefaults 와 Array  (0) 2017.09.22
텍스트 필드에서 숫자만 입력받기  (0) 2017.09.03
UITextView Placeholder  (0) 2017.08.19
HTTP Networking Framework - Alamofire  (0) 2016.02.03

+ Recent posts