我想做这样的事情,但我无法获得合作语法。
static const UIColor *colorNavbar = [UIColor colorWithRed: 197.0/255.0 green: 169.0/255.0 blue: 140.0/255.0 alpha: 1.0];
我想我可以定义宏,但它们很难看。
答案 0 :(得分:124)
我喜欢使用类别来扩展具有此类事物的新方法的类。这是我刚才写的代码的摘录:
@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;
}
你也可以让宏做备忘录。
答案 1 :(得分:35)
我通常为每个项目制作一个UIColor类别:
@interface UIColor (ProjectName)
+(UIColor *) colorForSomeTable;
+(UIColor *) colorForSomeControl;
+(UIColor *) colorForSomeText;
@end
使用实现中的常量:
@implementation UIColor (ProjectName)
+(UIColor *) colorForSomeTable { return [UIColor colorWithRed:...]; }
@end
我也会根据需要为UIFont和UIImage做同样的事情。
答案 2 :(得分:11)
要扩展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
无论如何都是不可变的。
答案 3 :(得分:11)
您可以定义'像这样的CONSTANT:
#define FAV_COLOR [UIColor colorWithRed:24/255.0f green:89/255.0f blue:36/255.0f alpha:0.9]
并按名称调用它,就像习惯常量一样:FAV_COLOR
希望有所帮助。
答案 4 :(得分:8)
你可以这样做:
#define backgroundColorApp [UIColor colorWithRed: 197.0/255.0 green: 169.0/255.0 blue: 140.0/255.0 alpha: 1.0]
答案 5 :(得分:5)
在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()
答案 6 :(得分:4)
#define textColorApp [UIColor colorWithRed: 197.0/255.0 green: 169.0/255.0 blue: 140.0/255.0 alpha: 1.0]
myLabel.textColor=textColorApp;
答案 7 :(得分:1)
只需在常量文件中定义下面的宏并仅传递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)];
答案 8 :(得分:0)
我觉得还值得一提的是另一个很少讨论的很棒的功能:Color Literals。它们不仅更易于阅读,而且更易于编辑。在斯威夫特,
let color: UIColor = #colorLiteral(red: 0.9607843137, green: 0.4784313725, blue: 0.3215686275, alpha:
粘贴到Xcode时,此语法会创建一个简单的颜色框。 Click here to see an example.
看到此框后,您可以双击它以轻松编辑它。同样,如果您的设计师有一个特定的颜色十六进制值列表,您可以在各种IB选项之间切换,包括RGB Sliders。