如何定义 UIColor 的常量值?
我想做这样的事情,但我无法获得合作语法。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我喜欢使用类别来通过新方法来扩展类来处理此类事情。这是我今天刚刚编写的代码摘录:
现在在代码中我可以执行以下操作:
我自己定义的颜色与系统定义的颜色完全一致。
(顺便说一句,我开始更多地考虑 HSB 而不是 RGB,因为我更关注颜色。)
关于预计算值的更新: 我的预感是这不值得。但如果你真的想要,你可以用静态变量来记忆这些值:
你也可以让一个宏来做记忆。
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:
Now in code I can do things like:
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:
You could make a macro do do the memoizing, too.
我通常为每个项目创建一个 UIColor 类别:
使用实现中的常量:
我也会根据需要对 UIFont 和 UIImage 执行相同的操作。
I usually make a category of UIColor for each project:
With the constants in the implementation:
I also do the same for UIFont and UIImage as needed.
如果您想预先计算值(或只执行一次),请扩展 jasoncrawford 的答案(我会将其作为注释放入,但您无法在注释中格式化代码)。
您最初的想法行不通的原因是编译器只能使用函数之外的初始化程序,而不能使用普通代码。您可以通过初始化方法实现类似于您想要的效果,例如
注意原始定义中的
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).
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.
NB the
const
qualifier on your original definition is redundant sinceUIColor
is immutable anyway.您可以像这样“定义”一个类似的常量:
并像您习惯的常量那样按名称调用它:
FAV_COLOR
希望有所帮助。
You can 'define' a similar CONSTANT like this:
and call it by name like you are used to with constants:
FAV_COLOR
Hope that helps.
你可以这样做:
You can do this:
在 Swift 中,定义一个扩展:
使用如下:
In Swift, define an extension:
Use like:
只需在常量文件中定义以下宏并仅传递 RGB 值并在您想要的任何地方使用
使用:
Just define below macro in your constant file and pass only RGB value and use anywhere you want
To use :
我觉得还值得一提的是另一个很少被谈论的很棒的功能:颜色文字。它们不仅更容易阅读,而且更容易编辑。在 Swift 中,
当粘贴到 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,
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.