How to create several cached UIColor
You can use the same approach as in
Using a dispatch_once singleton model in Swift, i.e. static
constant stored properties
which are initialized lazily (and only once). These can be defined
directly in the UIColor
extension:
extension UIColor {
convenience init(hex: String) {
// ...
}
static let myColorOne = UIColor(hex:"AABBCC")
static let myColorTwo = UIColor(hex:"DDEEFF")
}
Best practice for creating many UIColors?
First you should profile with Instruments to see if a considerable amount of time is spent in these calls. If this is the case, then make them static variables and initialize them on first use:
-(void)drawRect:(CGRect)rect
{
static UIColor *bgColor;
static UIColor *textColor;
static UIColor *strokeColor;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
bgColor = [UIColor colorWithWhite:0.9 alpha:1.0];
textColor = [UIColor colorWithWhite:0.98 alpha:1.0];
strokeColor = [UIColor colorWithWhite:0.6 alpha:1.0];
});
/* use colors */
}
Extension for UIColor with custom colors it is real?
Create class property in UIColor extension
extension UIColor
{
class var themeColor:UIColor {
return UIColor(red: 210.0/255.0, green: 105.0/255.0, blue: 130.0/255.0, alpha: 1.0)
}
}
OR
extension UIColor {
static let themeColor = UIColor(red: 210.0/255.0, green: 105.0/255.0, blue: 130.0/255.0, alpha: 1.0)
}
Usage
self.view.backgroundColor = UIColor.themeColor
How to define my own custom UIColor?
Try something like this:
extension UIColor {
static let myCustomColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
}
How to Convert a string into a UIColor (Swift)
UIColor named:
only works when you define a color set asset. Since your colors are defined in code, UIColor named:
will never return anything but nil
.
One solution is to put your colors into a dictionary instead of separate variables.
let colors: [String: UIColor] = [
"color1" : UIColor(red: 1, green: 153/255, blue: 0, alpha: 1),
"color2" : UIColor(red: 74/255, green: 134/255, blue: 232/255, alpha: 1),
"color3" : UIColor(red: 0, green: 0, blue: 1, alpha: 1),
"color4" : UIColor(red: 0, green: 1, blue: 0, alpha: 1),
"color5" : UIColor(red: 153/255, green: 0, blue: 1, alpha: 1),
"color6" : UIColor(red: 1, green: 0, blue: 0 , alpha: 1),
]
Then you can get your color as:
tile.color = colors["color\(value)"] ?? colors["color1"]!
Use Same Variable In Multiple View Controllers
One approach here is to create an extension
on UIColor
. You can then make them global to your module.
extension UIColor {
static var myRed: UIColor {
return UIColor(red:0.61, green:0.12, blue:0.20, alpha:1.0)
}
static var myBlue: UIColor {
return UIColor(red: 0.08, green: 0.49, blue: 0.49, alpha:1.0)
}
}
What is the UIColor of the default UITableView separator?
… in terms of CGContextSetRGBStrokeColor
it should be:
CGContextSetRGBStrokeColor (
CGContextRef c,
224.0/255.0,
224.0/255.0,
224.0/255.0,
CGFloat alpha
);
Quite simple and hopefully a solution for your problem.
Related Topics
Structs That Refer to Each Other in Swift 3
Using Non Ns_Enum Objective-C Enum in Swift
Swiftui List Empty State View/Modifier
Protocol Extending Encodable (Or Codable) Does Not Conform to It
Autolayout Contraints for a View from Xib
How to Rotate Only One View Controller to Landscape Orientation in iOS Swift 3
How to Use Alamofire with Custom Headers for Post Request
What Is the Advantage of a Lazy Var in Swift
Implementing Nscopying in Swift with Subclasses
How to Calculate the 21! (21 Factorial) in Swift
Listing All Files in a Folder Recursively with Swift
How to Use Sf Rounded Font in Swiftui
Sprite Kit Physicsbody.Resting Behavior
Uicollectionviewlayout Not Working with Uiimage in Swift 5 and Xcode 11
How to Install Xcode on an External Hard Drive Along with the iPhone Simulator.App