美文网首页iOS Developer
OC 十六进制色值转 RGB

OC 十六进制色值转 RGB

作者: 张小泺 | 来源:发表于2016-05-30 15:36 被阅读82次
    把十六进制的色值转换成 RGB
    + (UIColor *)colorWithHexColor:(NSString *)hexColorString {
        if ([hexColorString hasPrefix:@"#"]) {
            hexColorString = [hexColorString substringFromIndex:1];
        }
        
        if (hexColorString.length != 6) {
            return  nil;
        }
        
        NSRange range = NSMakeRange(0, 2);
        NSString *redStr = [hexColorString substringWithRange:range];
        range.location = 2;
        NSString *greenStr = [hexColorString substringWithRange:range];
        range.location = 4;
        NSString *blueStr = [hexColorString substringWithRange:range];
        
        unsigned int red, green, blue;
        [[NSScanner scannerWithString:redStr] scanHexInt:&red];
        [[NSScanner scannerWithString:greenStr] scanHexInt:&green];
        [[NSScanner scannerWithString:blueStr] scanHexInt:&blue];
        
        return [UIColor colorWithRed:red / 255.0f  green:green / 255.0f blue:blue / 255.0f alpha:1];
    }
    

    相关文章

      网友评论

        本文标题:OC 十六进制色值转 RGB

        本文链接:https://www.haomeiwen.com/subject/lgaqdttx.html