참고 : 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

간혹 무작위로 문자나 숫자를 추출해야 할 경우에 유용하게 쓰인다

참고 사이트 : https://learnappmaking.com/random-numbers-swift/

나는 임의의 문자열을 뽑기위한 코드를 자주 사용한다.

이 외에도 있으니 사이트를 참고하여 도움되기를 바란다.


func random(_ n: Int) -> String { let a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" var s = "" for _ in 0..<n { let r = Int(arc4random_uniform(UInt32(a.characters.count))) s += String(a[a.index(a.startIndex, offsetBy: r)]) } return s }

print(random(8)) // Output: 6FvUpkzp


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

Convert a hex string into a UIColor  (0) 2018.01.22
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