美工一般會(huì)給我們出16進(jìn)制的顏色值,就需要一個(gè)方法來(lái)實(shí)現(xiàn)了
1.我是這么實(shí)現(xiàn)的, 給UIColor寫個(gè)分類, 然后把頭文件 寫到導(dǎo)入到pch里面, 全局調(diào)用.不用每次都導(dǎo)入頭文件
/**
* 通過給定的顏色字符串生成指定的顏色
*/
+ (UIColor *)colorWithHexString:(NSString *)hexadecimal
{
const char *cString = [hexadecimal cStringUsingEncoding: NSASCIIStringEncoding];
long int hex;
if (cString[0] == '#')
{
hex = strtol(cString + 1, NULL, 16);
}
else
{
hex = strtol(cString, NULL, 16);
}
return [[self class] colorWithHex: (UInt32)hex];
}
+ (UIColor *)colorWithHex:(UInt32)hexadecimal
{
CGFloat red, green, blue;
red = (hexadecimal >> 16) & 0xFF;
green = (hexadecimal >> 8) & 0xFF;
blue = hexadecimal & 0xFF;
return [UIColor colorWithRed: red / 255.0f green: green / 255.0f blue: blue / 255.0f alpha: 1.0f];
}
調(diào)用方法:
cell.backgroundColor = [UIColor colorWithHexString:@"FF00FF"];
2.如果美工給的是RGB顏色值,我一般會(huì)在pch文件里統(tǒng)一設(shè)置, 簡(jiǎn)化代碼
//RGB顏色值
#define FLRGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
調(diào)用方法
lab.textColor = FLRGBColor(255, 133, 18);