将蓝湖取值#1B9E5CFF(HEXA)转为RGBA颜色值

项目中用到某些控件背景需要服务器控制返回值为字符串类似#1B9E5CFF

#define rgba(r,g,b,s)       [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:s]

/* 16进制转RGB  DLog(@"查看颜色:%@",[self getColor:@"#1B9E5C"]); */
+(UIColor *)getSixteenToRGBColor:(NSString *)hexColor {
//    [self getSixteenToRGBColor:@"#1B9E5CFF"];//
    unsigned int red,green,blue,aphex;
    hexColor = [hexColor stringByReplacingOccurrencesOfString:@"#" withString:@""];
   
    if (hexColor.length>=6) {
        NSRange range;
        range.length    = 2;
        
        range.location  = 0;
        [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&red];
        range.location  = 2;
        [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&green];
        range.location  = 4;
        [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&blue];
        
        aphex           = 1;
        
        if (hexColor.length==8) {
            range.location  = 6;
            [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&aphex];
        }
        
        DLog(@"RGB:%d-%d-%d-%d",red,green,blue,aphex);
        return rgba(red, green, blue, aphex);
    }else{
        
        return  UIColor.whiteColor;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容