网站建设资讯

NEWS

网站建设资讯

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

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

创新互联公司是一家专业提供黄埔企业网站建设,专注与做网站、成都网站制作、H5响应式网站、小程序制作等业务。10年已为黄埔众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。

- (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将16进制字符串转换成UIColor
本文地址:http://cdweb.net/article/gedged.html