Here's a simple extension to
UIColor that lets you create colors from hex strings. The new method is a failable initializer, which means it returns nil if you don't specify a color in the correct format. It should be a # symbol, followed by red, green, blue and alpha in hex format, for a total of nine characters. For example, #ffe700ff is gold.... Continue Reading >>
HTML color names let you use familiar titles like "steel blue" and "mint cream" rather than hex values, but sadly these standardized names aren't available in iOS – or at least not by default. Fortunately, it's easy to add an extension to
UIColor that maps these names to hexadecimal color values, then add another extension to convert hex colors to
UIColors. Here's the code:... Continue Reading >>
Although there are quite a few built-in UIColors, you'll want to create your own very frequently. This can be done in a number of ways, but the most common is specifying individual values for red, green, blue and alpha, like this:... Continue Reading >>
UIColor from red, green, blue, and alpha (RGBA) is easy enough:... Continue Reading >>
Nearly all subclasses of
UIView can have their background color adjusted, but often you’ll find you want to use an image rather than a flat color.... Continue Reading >>
About the Swift Knowledge Base
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions, all written for Swift 4.
Become a Swift expert
I wrote a book called Pro Swift that's dedicated to helping you master the Swift language, and it includes seven hours of video demonstrating each technique!