如何定义 UIColor 的常量值?

发布于 2024-08-30 13:14:42 字数 199 浏览 5 评论 0原文

我想做这样的事情,但我无法获得合作语法。

static const UIColor *colorNavbar = [UIColor colorWithRed: 197.0/255.0 green: 169.0/255.0 blue: 140.0/255.0 alpha: 1.0];

我想我可以定义宏,但它们很难看。

I want to do something like this, but I cannot get a cooperative syntax.

static const UIColor *colorNavbar = [UIColor colorWithRed: 197.0/255.0 green: 169.0/255.0 blue: 140.0/255.0 alpha: 1.0];

I suppose that I could define macros, but they are ugly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

烙印 2024-09-06 13:14:42

我喜欢使用类别来通过新方法来扩展类来处理此类事情。这是我今天刚刚编写的代码摘录:

@implementation UIColor (Extensions)

+ (UIColor *)colorWithHueDegrees:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness {
    return [UIColor colorWithHue:(hue/360) saturation:saturation brightness:brightness alpha:1.0];
}

+ (UIColor *)aquaColor {
    return [UIColor colorWithHueDegrees:210 saturation:1.0 brightness:1.0];
}

+ (UIColor *)paleYellowColor {
    return [UIColor colorWithHueDegrees:60 saturation:0.2 brightness:1.0];
}

@end

现在在代码中我可以执行以下操作:

self.view.backgroundColor = highlight? [UIColor paleYellowColor] : [UIColor whitecolor];

我自己定义的颜色与系统定义的颜色完全一致。

(顺便说一句,我开始更多地考虑 HSB 而不是 RGB,因为我更关注颜色。)

关于预计算值的更新: 我的预感是这不值得。但如果你真的想要,你可以用静态变量来记忆这些值:

+ (UIColor *)paleYellowColor {
    static UIColor *color = nil;
    if (!color) color = [UIColor colorWithHueDegrees:60 saturation:0.2 brightness:1.0];
    return color;
}

你也可以让一个宏来做记忆。

I like to use categories to extend classes with new methods for this sort of thing. Here's an excerpt of code I just wrote today:

@implementation UIColor (Extensions)

+ (UIColor *)colorWithHueDegrees:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness {
    return [UIColor colorWithHue:(hue/360) saturation:saturation brightness:brightness alpha:1.0];
}

+ (UIColor *)aquaColor {
    return [UIColor colorWithHueDegrees:210 saturation:1.0 brightness:1.0];
}

+ (UIColor *)paleYellowColor {
    return [UIColor colorWithHueDegrees:60 saturation:0.2 brightness:1.0];
}

@end

Now in code I can do things like:

self.view.backgroundColor = highlight? [UIColor paleYellowColor] : [UIColor whitecolor];

and my own defined colors fit right in alongside the system-defined ones.

(Incidentally, I am starting to think more in terms of HSB than RGB as I pay more attention to colors.)

UPDATE regarding precomputing the value: My hunch is that it's not worth it. But if you really wanted, you could memoize the values with static variables:

+ (UIColor *)paleYellowColor {
    static UIColor *color = nil;
    if (!color) color = [UIColor colorWithHueDegrees:60 saturation:0.2 brightness:1.0];
    return color;
}

You could make a macro do do the memoizing, too.

一抹淡然 2024-09-06 13:14:42

我通常为每个项目创建一个 UIColor 类别:

@interface UIColor (ProjectName)

+(UIColor *) colorForSomeTable;
+(UIColor *) colorForSomeControl;
+(UIColor *) colorForSomeText;

@end

使用实现中的常量:

@implementation UIColor (ProjectName)

+(UIColor *) colorForSomeTable { return [UIColor colorWithRed:...]; }

@end

我也会根据需要对 UIFont 和 UIImage 执行相同的操作。

I usually make a category of UIColor for each project:

@interface UIColor (ProjectName)

+(UIColor *) colorForSomeTable;
+(UIColor *) colorForSomeControl;
+(UIColor *) colorForSomeText;

@end

With the constants in the implementation:

@implementation UIColor (ProjectName)

+(UIColor *) colorForSomeTable { return [UIColor colorWithRed:...]; }

@end

I also do the same for UIFont and UIImage as needed.

夏花。依旧 2024-09-06 13:14:42

如果您想预先计算值(或只执行一次),请扩展 jasoncrawford 的答案(我会将其作为注释放入,但您无法在注释中格式化代码)。

+ (UIColor *)paleYellowColor
{
    static UIColor* paleYellow = nil;
    if (paleYellow == nil)
    {
        paleYellow = [UIColor colorWithHueDegrees:60 saturation:0.2 brightness:1.0];
    }
    return paleYellow;
}

您最初的想法行不通的原因是编译器只能使用函数之外的初始化程序,而不能使用普通代码。您可以通过初始化方法实现类似于您想要的效果,例如

static UIColor* colorNavBar = nil;

+(void) initialize
{
    if (colorNavBar != nil)
    {
        colorNavBar = ....
    }
}

注意原始定义中的 const 限定符是多余的,因为 UIColor 无论如何都是不可变的。

To expand on jasoncrawford's answer (I'd put this in as a comment, but you can't format code in the comments) if you want to precompute the values (or do it only once).

+ (UIColor *)paleYellowColor
{
    static UIColor* paleYellow = nil;
    if (paleYellow == nil)
    {
        paleYellow = [UIColor colorWithHueDegrees:60 saturation:0.2 brightness:1.0];
    }
    return paleYellow;
}

The reason your original idea doesn't work is because the compiler can only use initialisers outside of functions, not normal code. You could have achieved something like what you wanted with the initialize methosd e.g.

static UIColor* colorNavBar = nil;

+(void) initialize
{
    if (colorNavBar != nil)
    {
        colorNavBar = ....
    }
}

NB the const qualifier on your original definition is redundant since UIColor is immutable anyway.

情徒 2024-09-06 13:14:42

您可以像这样“定义”一个类似的常量:

#define FAV_COLOR [UIColor colorWithRed:24/255.0f green:89/255.0f blue:36/255.0f alpha:0.9]

并像您习惯的常量那样按名称调用它:FAV_COLOR

希望有所帮助。

You can 'define' a similar CONSTANT like this:

#define FAV_COLOR [UIColor colorWithRed:24/255.0f green:89/255.0f blue:36/255.0f alpha:0.9]

and call it by name like you are used to with constants: FAV_COLOR

Hope that helps.

漫雪独思 2024-09-06 13:14:42

你可以这样做:

#define backgroundColorApp [UIColor colorWithRed: 197.0/255.0 green: 169.0/255.0 blue: 140.0/255.0 alpha: 1.0]

You can do this:

#define backgroundColorApp [UIColor colorWithRed: 197.0/255.0 green: 169.0/255.0 blue: 140.0/255.0 alpha: 1.0]
甜宝宝 2024-09-06 13:14:42

在 Swift 中,定义一个扩展:

extension UIColor {
    
   class func XXXWhiteColor() -> UIColor {
        return UIColor(red: 256, green: 256, blue: 256, alpha: 1.0)
    }
 
    class func XXXGreenColor() -> UIColor {
        return UIColor(red: 73/255.0, green: 212/255.0, blue: 86/255.0, alpha: 1.0)
    }
}

使用如下:

label.background = UIColor.XXXWhiteColor()

In Swift, define an extension:

extension UIColor {
    
   class func XXXWhiteColor() -> UIColor {
        return UIColor(red: 256, green: 256, blue: 256, alpha: 1.0)
    }
 
    class func XXXGreenColor() -> UIColor {
        return UIColor(red: 73/255.0, green: 212/255.0, blue: 86/255.0, alpha: 1.0)
    }
}

Use like:

label.background = UIColor.XXXWhiteColor()
樱&纷飞 2024-09-06 13:14:42
#define textColorApp [UIColor colorWithRed: 197.0/255.0 green: 169.0/255.0 blue: 140.0/255.0 alpha: 1.0]

myLabel.textColor=textColorApp;
#define textColorApp [UIColor colorWithRed: 197.0/255.0 green: 169.0/255.0 blue: 140.0/255.0 alpha: 1.0]

myLabel.textColor=textColorApp;
半衬遮猫 2024-09-06 13:14:42

只需在常量文件中定义以下宏并仅传递 RGB 值并在您想要的任何地方使用

#define RGBCOLOR(r,g,b)[UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]

使用:

[lblCount setTextColor:RGBCOLOR(236, 43, 92)];

Just define below macro in your constant file and pass only RGB value and use anywhere you want

#define RGBCOLOR(r,g,b)[UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]

To use :

[lblCount setTextColor:RGBCOLOR(236, 43, 92)];
酒废 2024-09-06 13:14:42

我觉得还值得一提的是另一个很少被谈论的很棒的功能:颜色文字。它们不仅更容易阅读,而且更容易编辑。在 Swift 中,

let color: UIColor = #colorLiteral(red: 0.9607843137, green: 0.4784313725, blue: 0.3215686275, alpha: 

当粘贴到 Xcode 中时,此语法会创建一个简单的颜色框。
单击此处查看示例。

看到该框后,您可以双击它轻松编辑它。同样,如果您有特定的列表,您可以在各种 IB 选项之间切换,包括 RGB 滑块来自设计师的颜色十六进制值。

I feel it's also worth mentioning another awesome feature that is rarely talked about: Color Literals. Not only are they easier to read, but they are WAY easier to edit. In Swift,

let color: UIColor = #colorLiteral(red: 0.9607843137, green: 0.4784313725, blue: 0.3215686275, alpha: 

When pasted into Xcode, this syntax creates a simple color box.
Click here to see an example.

Once you see the box, you can then double click on it to edit it easily. Similarly, you can switch between various IB options, including RGB Sliders, if you have a specific list of color hex values from your designer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文