iOS颜色值

美工一般会给我们出16进制的颜色值,就需要一个方法来实现了

1.我是这么实现的, 给UIColor写个分类, 然后把头文件 写到导入到pch里面, 全局调用.不用每次都导入头文件
/**
 * 通过给定的颜色字符串生成指定的颜色
 */
+ (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];
}


调用方法:

        cell.backgroundColor = [UIColor colorWithHexString:@"FF00FF"];
2.如果美工给的是RGB颜色值,我一般会在pch文件里统一设置, 简化代码
//RGB颜色值
#define FLRGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]

调用方法

        lab.textColor = FLRGBColor(255, 133, 18);

你可能感兴趣的:(iOS颜色值)