IOS直接使用美工给的16位制颜色代码
2016-08-15 本文已影响68人
lxf_2013
加入一个分类:
#import <UIKit/UIKit.h>
@interface UIColor (ColorWithString)
+(UIColor *)colorWithString:(NSString *)string;
@end
#import "UIColor+ColorWithString.h"
@implementation UIColor (ColorWithString)
+(UIColor *)colorWithString:(NSString *)string{
if ([string isKindOfClass:[NSString class]]) {
string = [string stringByReplacingOccurrencesOfString:@"'" withString:@""];
if ([string hasPrefix:@"#"]) {
const char *s = [string cStringUsingEncoding:NSASCIIStringEncoding];
if (*s == '#') {
++s;
}
unsigned long long value = strtoll(s, nil, 16);
int r, g, b, a;
switch (strlen(s)) {
case 2:
// xx
r = g = b = (int)value;
a = 255;
break;
case 3:
// RGB
r = ((value & 0xf00) >> 8);
g = ((value & 0x0f0) >> 4);
b = ((value & 0x00f) >> 0);
r = r * 16 + r;
g = g * 16 + g;
b = b * 16 + b;
a = 255;
break;
case 6:
// RRGGBB
r = (value & 0xff0000) >> 16;
g = (value & 0x00ff00) >> 8;
b = (value & 0x0000ff) >> 0;
a = 255;
break;
default:
// RRGGBBAA
r = (value & 0xff000000) >> 24;
g = (value & 0x00ff0000) >> 16;
b = (value & 0x0000ff00) >> 8;
a = (value & 0x000000ff) >> 0;
break;
}
return [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a/255.0f];
}
else
{
string = [string stringByAppendingString:@"Color"];
SEL colorSel = NSSelectorFromString(string);
if ([UIColor respondsToSelector:colorSel]) {
return [UIColor performSelector:colorSel];
}
return nil;
}
} else if([string isKindOfClass:[UIColor class]]){
return (UIColor *)string;
}
return nil;
}
导入这个分类,然后直接使用:
//例子
view.backgroundColor = [UIColor colorWithString:@"#EBEBF1"];