-
-
Save sauvikatinnofied/92c25739d4ae7bb5cc45e6b75fd63fe7 to your computer and use it in GitHub Desktop.
| import Foundation | |
| import UIKit | |
| // Usage Examples | |
| let shadowColor = Color.shadow.value | |
| let shadowColorWithAlpha = Color.shadow.withAlpha(0.5) | |
| let customColorWithAlpha = Color.custom(hexString: "#123edd", alpha: 0.25).value | |
| enum Color { | |
| case theme | |
| case border | |
| case shadow | |
| case darkBackground | |
| case lightBackground | |
| case intermidiateBackground | |
| case darkText | |
| case lightText | |
| case intermidiateText | |
| case affirmation | |
| case negation | |
| case custom(hexString: String, alpha: Double) | |
| func withAlpha(_ alpha: Double) -> UIColor { | |
| return self.value.withAlphaComponent(CGFloat(alpha)) | |
| } | |
| } | |
| extension Color { | |
| var value: UIColor { | |
| var instanceColor = UIColor.clear | |
| switch self { | |
| case .border: | |
| instanceColor = UIColor(hexString: "#333333") | |
| case .theme: | |
| instanceColor = UIColor(hexString: "#ffcc00") | |
| case .shadow: | |
| instanceColor = UIColor(hexString: "#ccccc") | |
| case .darkBackground: | |
| instanceColor = UIColor(hexString: "#999966") | |
| case .lightBackground: | |
| instanceColor = UIColor(hexString: "#cccc66") | |
| case .intermidiateBackground: | |
| instanceColor = UIColor(hexString: "#cccc99") | |
| case .darkText: | |
| instanceColor = UIColor(hexString: "#333333") | |
| case .intermidiateText: | |
| instanceColor = UIColor(hexString: "#999999") | |
| case .lightText: | |
| instanceColor = UIColor(hexString: "#cccccc") | |
| case .affirmation: | |
| instanceColor = UIColor(hexString: "#00ff66") | |
| case .negation: | |
| instanceColor = UIColor(hexString: "#ff3300") | |
| case .custom(let hexValue, let opacity): | |
| instanceColor = UIColor(hexString: hexValue).withAlphaComponent(CGFloat(opacity)) | |
| } | |
| return instanceColor | |
| } | |
| } | |
| extension UIColor { | |
| /** | |
| Creates an UIColor from HEX String in "#363636" format | |
| - parameter hexString: HEX String in "#363636" format | |
| - returns: UIColor from HexString | |
| */ | |
| convenience init(hexString: String) { | |
| let hexString: String = (hexString as NSString).trimmingCharacters(in: .whitespacesAndNewlines) | |
| let scanner = Scanner(string: hexString as String) | |
| if hexString.hasPrefix("#") { | |
| scanner.scanLocation = 1 | |
| } | |
| var color: UInt32 = 0 | |
| scanner.scanHexInt32(&color) | |
| let mask = 0x000000FF | |
| let r = Int(color >> 16) & mask | |
| let g = Int(color >> 8) & mask | |
| let b = Int(color) & mask | |
| let red = CGFloat(r) / 255.0 | |
| let green = CGFloat(g) / 255.0 | |
| let blue = CGFloat(b) / 255.0 | |
| self.init(red:red, green:green, blue:blue, alpha:1) | |
| } | |
| } |
That was helpful, thank you.
Tell me one thing Souvik da, suppose in my application, I have 10 screen and all the screen have same background color and I set it using storyBoard background color option, then how can I manage this if client say to change background color?
I want to change it in one place and effect will show to all 10 screen. Is it possible? I don't want go programatically.
Tell me one thing Souvik da, suppose in my application, I have 10 screen and all the screen have same background color and I set it using storyBoard background color option, then how can I manage this if client say to change background color?
I want to change it in one place and effect will show to all 10 screen. Is it possible? I don't want go programatically.
I think you can do it by adding your custom color in assets. just google it.
I think you can do it by adding your custom color in assets. just google it.
Color assets are a solution for this but they are only available on iOS 11+ and macOS 10.13+ . To support older versions you will have to do it in code.
Great idea!
Since Swift 3, I tend to prefer
with(alpha:)overwithAlpha(_:)