Created
September 10, 2018 21:39
-
-
Save erdemarslan/5590a9d40431a93168baf4c8ad4660bc to your computer and use it in GitHub Desktop.
UIColor ile ilgili kullanışlı extensionlarım
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UIColor { | |
func fromHex(_ hex : String) -> UIColor { | |
var hexString = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased() | |
if hexString.hasPrefix("#") { | |
hexString.remove(at: hexString.startIndex) | |
} | |
if hexString.count != 6 { | |
return UIColor.black | |
} | |
var rgb : UInt32 = 0 | |
Scanner(string: hexString).scanHexInt32(&rgb) | |
return UIColor.init(red: CGFloat((rgb & 0xFF0000) >> 16) / 255.0, | |
green: CGFloat((rgb & 0x00FF00) >> 8) / 255.0, | |
blue: CGFloat(rgb & 0x0000FF) / 255.0, | |
alpha: 1.0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment