iOS 将16进制字符串转换成UIColor

#pragma mark - 16进制字符串转换成 UIColor

- (UIColor *)generateColorObjectWithHex:(NSString *)hexString

{

    unsigned int red = 0, green = 0, blue = 0;

    NSRange range = {0, 2};

    range.location = 0;

    

    NSString *redString = [hexString substringWithRange:range];

    NSScanner *redScanner = [NSScanner scannerWithString:redString];

    [redScanner scanHexInt:&red];

    

    range.location = 2;

    NSScanner *greenScanner = [NSScanner scannerWithString:[hexString substringWithRange:range]];

    [greenScanner scanHexInt:&green];

    

    range.location = 4;

    NSScanner *blueScanner = [NSScanner scannerWithString:[hexString substringWithRange:range]];

    [blueScanner scanHexInt:&blue];

    

    __autoreleasing UIColor *color = [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1];

    return color;

}


你可能感兴趣的:(ios,UIColor,16进制)