为应用程序中常用的颜色等创建实用程序类

发布于 2024-11-01 19:31:34 字数 141 浏览 2 评论 0原文

我正在制作一个实用程序类,该类在其他常用代码中具有一些返回颜色的方法。然而 UIColor 类是 UIKit 的一部分,所以我想知道我应该将 UIKit 导入到 NSObject 的这个子类中,还是应该返回一个 id ?或者还有其他选择吗?

提前致谢。

I am making a utilities class that among other frequently used code have some methods for returning colors. However UIColor class is a part of UIKit so I wonder should I import UIKit to this subclass of NSObject, or should I return an id? Or are there other options?

Thanks in advance.

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

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

发布评论

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

评论(3

情徒 2024-11-08 19:31:34

我使用 UIColor 类别来代替子类来自定义颜色,

如下所示:

@implementation UIColor (CustomColors)

+ (UIColor *)mb_toolBarTintColor {
    return [UIColor colorWithHue:0.5 saturation:0.1 brightness:0.3 alpha:1];
}

@end

然后我可以将它与简单的

[self.toolBar setTintColor:[UIColor mb_toolBarTintColor]];

Instead of a subclass I use a UIColor category for custom colors

something like this:

@implementation UIColor (CustomColors)

+ (UIColor *)mb_toolBarTintColor {
    return [UIColor colorWithHue:0.5 saturation:0.1 brightness:0.3 alpha:1];
}

@end

and then I can use it with a simple

[self.toolBar setTintColor:[UIColor mb_toolBarTintColor]];
萌无敌 2024-11-08 19:31:34

子类总是需要导入父类。让您的子类成为 UIColor 的直接子类,然后导入超类或 .h 文件中的整个套件。

A subclass always needs to import the super. Make your subclass a direct subclass of UIColor, and then import the superclass, or the entire kit in the .h file.

海的爱人是光 2024-11-08 19:31:34

您还可以简单地创建一个 .h 文件,其中包含一堆 #define 常量值,例如:

#define TEXT_COLOR [UIColor colorWithRed:0.8f green:0.8f blue:0.8f alpha:1.0f]

You can also simply make a .h file that has a bunch of #define values for constants, such as:

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