Custom Colors For Your Mobile Application.

Joshua
therisecollection
Published in
2 min readJun 23, 2019

Introduction

Apple’s UIColor, gives you easy access to a multitude of colors and they are fairly easy to use! For example if you quickly wanted to change your label text red it would be done like this. Have you ever thought about giving your application a little bit of a more personal touch using a custom color?? If so don’t go anywhere, here is a quick and easy way to implement your desired custom color!

label.textColor = UIColor.red

Without having to change the opacity of UIColor.red, let me introduce you to something every Apple Developer should know about.. Structs.

What is a Struct and how is it different from a Class?

Classes and structs in Swift are basic constructs of your program. In Swift, both classes and structs can have properties and functions. The key difference is structs are value types and classes are reference types.

Implementation

Start by creating a new file in your project, Colors.swift is a great name but feel free to be creative!

Step 1: Create the Struct

import UIKitextension UIColor { 
struct customColors {
Leave space for code here }
}

Step 2: Figure out the RGB values of your colors

The easiest way recommended to retrieve custom RGB color values, goes like this.. Find your color code #ffffff — white, #8b00b2 — light purple… After finding the color you want visit this link to grab your RGB values!

Step 3: Implement your custom colors

import UIKitextension UIColor { 
struct customColors {
#8b00b2 EXAMPLE - var lightPurple = UIColor(red: 0.5451, green: 0, blue: 0.698, alpha: 1.0) }
}

How are the custom colors accessed in code?

Access your custom colors through dot notation! “structName.varName” in the example above the code would look like this.

label.textColor = UIColor.customColors.lightPurple

Conclusion

Adding your personal favorite color to the application wasn’t that hard! Have fun customizing your application, going the extra mile and experimenting with custom colors can improve the overall look of your UI so have fun with it!

--

--

Joshua
therisecollection

“When you don’t create things, you become defined by your tastes rather than ability. So create.” 👨‍💻 Founder | therisecollection