通过您的问题的文字,您想要快捷方式并不是很清楚 - 一种颜色,创建带有值的 RGBA 颜色[0..255]
?
DrummerB 第一个回答,Justin Boo 第二个回答。
我想提出一个适合两者的解决方案:
创建一个可以涵盖两者的类别
[UIColor colorWith255ValuesWithRed: 128 green: 35 blue: 40 alpha:255]
,包装方法,您现在正在使用
- 创建一个 UIColor 类方法,将 UIColor 对象存储在静态 NSMutableDictionary 及其名称和对应部分中,您可以这样调用
[UIColor registeredColorWithName:@"activeForegroundColor"]
我为颜色寄存器的想法写了一个示例代码:
UIColor+Register.h
#import <UIKit/UIKit.h>
@interface UIColor (Register)
+(void)registerColor:(UIColor *)color
forName:(NSString *)name;
+(UIColor *)registeredColorForName:(NSString *)name;
+(void)unregisterColorForName:(NSString *)name;
@end
UIColor+Register.m
#import "UIColor+Register.h"
@interface UIColor (RegisterPrivate)
+(NSMutableDictionary *)colorRegister;
@end
@implementation UIColor (RegisterPrivate)
+(NSMutableDictionary *)colorRegister
{
static dispatch_once_t once;
static NSMutableDictionary *register_;
dispatch_once(&once, ^{
register_ = [NSMutableDictionary dictionary];
});
return register_;
}
@end
@implementation UIColor (Register)
+(void)registerColor:(UIColor *)color
forName:(NSString *)name
{
[[self colorRegister] setObject:color forKey:name];
}
+(UIColor *)registeredColorForName:(NSString *)name
{
return [[self colorRegister] objectForKey:name];
}
+(void)unregisterColorForName:(NSString *)name
{
[[self colorRegister] removeObjectForKey:name];
}
@end
用法:
注册
[UIColor registerColor:[UIColor redColor] forName:@"activeColor"];
[UIColor registerColor:[UIColor grayColor] forName:@"passiveColor"];
使用权
[view1 setBackgroundColor:[UIColor registeredColorForName:@"passiveColor"]];
[view2 setBackgroundColor:[UIColor registeredColorForName:@"activeColor"]];
注销
[UIColor unregisterColorForName:@"activeColor"];