Preface
During the Spring Festival, I made a new product, OneScreen, which summarized some skills since Swift development. Today, I will bring you a technique to facilitate color retrieval, better use, and modify colors at any time.
The main contents are:
1. By extension, mark the color using hexadecimal color code
2. Build custom colors/styles to be easy to call on each page
3. In subsequent UI adjustments, you only need to adjust one file to preview the global
In fact, the tips of 2 and 3 are similar to the multi-theme solution I have shared before.
1. Use hexadecimal color code
In the development of OneScreen, the file is first created, and the following code can implement our subsequent call to the hexadecimal color code.
import Foundation extension UIColor { class func colorWithHexString(hex:String) ->UIColor { var cString = (in:).uppercased() if (("#")) { let index = (, offsetBy:1) cString = (from: index) } if ( != 6) { return } let rIndex = (, offsetBy: 2) let rString = (to: rIndex) let otherString = (from: rIndex) let gIndex = (, offsetBy: 2) let gString = (to: gIndex) let bIndex = (, offsetBy: -2) let bString = (from: bIndex) var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0; Scanner(string: rString).scanHexInt32(&r) Scanner(string: gString).scanHexInt32(&g) Scanner(string: bString).scanHexInt32(&b) return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(1)) } }
In this way, we pass(hex: "#______")
You can call colors and get colors faster.
2. Build your own color
Next, I created the color library used in all pages and directly created all the colors to be used in the file. It is also convenient to remember and use appropriately for each color.
import Foundation import UIKit struct Theme{ static var ThemeBlue:UIColor = (hex: "#46b8ee") static var ThemeDarkBlue:UIColor = (hex: "#3eb5ed") static var ThemeDeepBlue:UIColor = (hex: "#2396cd") static var ThemePurple:UIColor = (hex: "#8267c6") static var ThemeDarkPurple:UIColor = (hex: "#7963c5") static var ThemeDeepPurple:UIColor = (hex: "#7059c5") //... }
3. Call on each page
The calling process is very simple. You just need to directly follow the name of the color after the Theme where we need UIColor, for example:
= =
With two simple files, you can get colors and customize colors faster. When we need to add or change the color in the subsequent UI adjustment, we only need to change the code. Especially the existing colors can be updated to the latest colors without any changes in other page files.
I hope such a solution can improve your development efficiency.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.