Tutorial by Examples

There are many ways you can create a UIColor: Swift Using one of the predefined colors: let redColor = UIColor.redColor() let blueColor: UIColor = .blueColor() // In Swift 3, the "Color()" suffix is removed: let redColor = UIColor.red let blueColor: UIColor = .blue If the c...
There are a variety of undocumented methods on UIColorwhich expose alternate colors or functionality. These can be found in the UIColor private header file. I will document the use of two private methods, styleString() and _systemDestructiveTintColor(). styleString Since iOS 2.0 there is a priva...
You can set the opacity to a certain UIColor without creating a new one using the init(red:_,green:_,blue:_,alpha:_) initializer. Swift let colorWithAlpha = UIColor.redColor().colorWithAlphaComponent(0.1) Swift 3 //In Swift Latest Version _ colorWithAlpha = UIColor.red.withAlphaComponent(0.1)...
By default, Interface Builder doesn't accept the CGColor datatype, so to allow adding a CGColor using user defined attributes in interface builder; one may want to use an extension like this: Swift Extension : extension CALayer { func borderUIColor() -> UIColor? { return borderCol...
You can create a UIColor from a hexadecimal number or string, e.g. 0xff00cc, "#FFFFFF" Swift Int Value extension UIColor { convenience init(hex: Int, alpha: CGFloat = 1.0) { let r = CGFloat((hex >> 16) & 0xff) / 255 let g = CGFloat((hex >> 08) &amp...
The below code example will give you an adjusted version of that color where a higher percentage will be brighter and a lower percentage will be darker. Objective-C + (UIColor *)adjustedColorForColor:(UIColor *)c : (double)percent { if (percent < 0) percent = 0; CGFloat r, g, b...
You can create a UIColor object using an image pattern by using the UIColor(patternImage:_) method. btn.backgroundColor = UIColor(patternImage: UIImage(named: "image")!)
The code example below demonstrate how you can get a lighter and darker shade of a given color, useful in applications having dynamic themes For Darker Color + (UIColor *)darkerColorForColor:(UIColor *)c { CGFloat r, g, b, a; if ([c getRed:&r green:&g blue:&b alpha:&a]) ...

Page 1 of 1