iOS 通过16进制计算颜色

来源:互联网 发布:新浪微博pv和uv数据 编辑:程序博客网 时间:2024/06/04 20:05

+ (UIColor *)colorFromHexRGB:(NSString *)inColorString

{

    UIColor *result = nil;

    unsigned int colorCode = 0;

    unsigned char redByte, greenByte, blueByte;

    

    if (nil != inColorString)

    {

        NSScanner *scanner = [NSScanner scannerWithString:inColorString];

        (void) [scanner scanHexInt:&colorCode]; // ignore error

    }

    redByte = (unsigned char) (colorCode >> 16);

    greenByte = (unsigned char) (colorCode >> 8);

    blueByte = (unsigned char) (colorCode); // masks off high bits

    result = [UIColor

              colorWithRed: (float)redByte / 0xff

              green: (float)greenByte/ 0xff

              blue: (float)blueByte / 0xff

              alpha:1.0];

    return result;

}

0 0
原创粉丝点击