iOS 颜色支持色号

新建类别 UIColor+Hex


UIColor+Hex.h文件

+ (instancetype)colorWithHex:(NSInteger)hex;

+ (instancetype)colorWithHex:(NSInteger)hex alpha:(double)alpha;


UIColor+Hex.m文件

+ (instancetype)colorWithHex:(NSInteger)hex {

    return [UIColor colorWithHex:hex alpha:1];

}


+ (instancetype)colorWithHex:(NSInteger)hex alpha:(double)alpha{

    NSInteger r = hex / 0x10000;

    NSInteger g = (hex - r * 0x10000) / 0x100;

    NSInteger b = (hex - r * 0x10000 - g * 0x100);

    return [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:alpha];

}



使用方法

self.view.backgroundColor = [UIColor colorWithHex:0xde2f51];

你可能感兴趣的:(颜色,背景)