Extension for Uicolor with Custom Colors It Is Real

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 access extension of UIColor in Swift?

You have defined an instance method, which means that you can call
it only on an UIColor instance:

let col = UIColor().getCustomBlueColor()
// or in your case:
btnShare.setTitleColor(UIColor().getCustomBlueColor(), forState: .Normal)

The compiler error "missing argument" occurs because
Instance Methods are Curried Functions in Swift,
so it could equivalently be called as

let col = UIColor.getCustomBlueColor(UIColor())()

(But that would be a strange thing to do, and I have added it only to
explain where the error message comes from.)


But what you really want is a type method (class func)

extension UIColor{
class func getCustomBlueColor() -> UIColor{
return UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00)
}
}

which is called as

let col = UIColor.getCustomBlueColor()
// or in your case:
btnShare.setTitleColor(UIColor.getCustomBlueColor(), forState: .Normal)

without the need to create an UIColor instance first.

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)

}

UIColor extension for .cgColor colors?

Try with this

extension CGColor{
public class var customRed: CGColor {
return UIColor(red: 244/255, green: 59/255, blue: 59/255, alpha: 1).cgColor
}
}

Use it

self.view.layer.borderColor = CGColor.customRed

Hope this helps you

Add custom .colorNames to UIColor somehow?

Those aren't really color shortcuts. That's just inferring a type. The fontColor property is typed as UIColor, and UIColor has a bunch of read-only properties on the class that are color names and return color objects. So when you say

myLabel.fontColor = .myCustomColor

The dot tells it "it is a field somewhere, guess where" and Swift goes, well, I need a UIColor, so let's look if the UIColor class has properties of that name that return the right type.

So to add your own, you'd have to define your color properties in an extension on UIColor.

extension UIColor {
static let con_pink = UIColor( red: 1.0, green: 0.0, blue: 0.5, alpha: 1.0 )
}

and those you can use as myLabel.fontColor = .con_pink.

But note that, if you do this, you're risking collisions with any color methods Apple might add in the future. So I recommend that you add a prefix to the property names (I chose "con_" for you based on your username), to make it less likely that Apple use the same name.

What is the best way to introduce a custom UIColor to a Swift project?

Answer: Extension, in my professional opinion.

Think about it; you are, in philosophy, 'extending' the range of colors offered by UIColor. Provided that your color name is distinct and the new function follows Apple's method naming protocol (i.e. <color name>Color), extending UIColor seems neater. One or two new colors (in my opinion) don't warrant an entire dedicated struct.


Bonus answer:
Where would a struct (or enum!) be a good fit?

  • If your app forces replacements for the standard colors (i.e. custom
    'primary' colors)
  • If your app is specifically designed to be themed / customized, it might be good have an enum, to serve as a concrete list for available options.
  • If you can't think of standard names for the colors (sharkBlueColor, anyone?).
  • If your app is specifically for drawing/painting (in which case a 'palette' construct might be good idea).

... the list goes on. You must learn to discern and decide for yourself as you mature as a Swift developer!

how get an RGB color in swift?

your answer is

extension UIColor {

static func rgb(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)->UIColor{
return UIColor(red: red/255, green: green/255, blue: blue/255, alpha: alpha)

}

static func colorA() -> UIColor {
return UIColor(red: 146/255, green: 39/255, blue: 143/255, alpha: 1)
}

static func colorB() -> UIColor {
return UIColor(red: 173/255, green: 104/255, blue: 173/255, alpha: 1)
}
}

How to get random warm/cold color in UIColor?

Fogmeister's answer meets the technical requirement, but it has some problems. For a warm color, if red is low enough, you won't see it. The blue component of a warm color may be so close to the red that there's no visible difference, etc.

I'd suggest this instead:

extension UIColor {
static var warm: UIColor {
let red: CGFloat = .random(in: 0.6...1) /Force red to be at least 0.6
let blue: CGFloat = .random(in: 0..<0.5) //Force blue to be smaller
return UIColor(red: red, green: .random(in: 0...1), blue: blue, alpha: 1)
}

static var cool: UIColor {
let blue: CGFloat = .random(in: 0.6..<1.0) //Force blue to be > 0.6
let red: CGFloat = .random(in: 0...0.5) //Force red to be smaller
return UIColor(red: red, green: .random(in: 0...1), blue: blue, alpha: 1)
}
}

How do I easily support light and dark mode with a custom color used in my app?

As it turns out, this is really easy with the new UIColor init(dynamicProvider:) initializer.

Update the custom color to:

extension UIColor {
static var myControlBackground: UIColor {
if #available(iOS 13.0, *) {
return UIColor { (traits) -> UIColor in
// Return one of two colors depending on light or dark mode
return traits.userInterfaceStyle == .dark ?
UIColor(red: 0.5, green: 0.4, blue: 0.3, alpha: 1) :
UIColor(red: 0.3, green: 0.4, blue: 0.5, alpha: 1)
}
} else {
// Same old color used for iOS 12 and earlier
return UIColor(red: 0.3, green: 0.4, blue: 0.5, alpha: 1)
}
}
}

That's it. No need to define two separate statics. The control class doesn't need any changes from the original code. No need to override traitCollectionDidChange or anything else.

The nice thing about this is that you can see the color change in the app switcher immediately after changing the mode in the Settings app. And of course the color is up-to-date automatically when you go back to the app.

On a related note when supporting light and dark mode - Use as many of the provided colors from UIColor as possible. See the available dynamic colors from UI Elements and Standard Colors. And when you need your own app-specific colors to support both light and dark mode, use the code in this answer as an example.


In Objective-C, you can define your own dynamic colors with:

UIColor+MyApp.h:

@interface UIColor (MyApp)

@property (class, nonatomic, readonly) UIColor *myControlBackgroundColor;

@end

UIColor+MyApp.m:

+ (UIColor *)myControlBackgroundColor {
if (@available(iOS 13.0, *)) {
return [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traits) {
return traits.userInterfaceStyle == UIUserInterfaceStyleDark ?
[self colorWithRed:0.5 green:0.4 blue:0.2 alpha:1.0] :
[self colorWithRed:0.3 green:0.4 blue:0.5 alpha:1.0];
}];
} else {
return [self colorWithRed:0.3 green:0.4 blue:0.5 alpha:1.0];
}
}


Related Topics



Leave a reply



Submit