美文网首页初见
将蓝湖取值#1B9E5CFF(HEXA)转为RGBA颜色值

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

作者: selice | 来源:发表于2019-12-18 10:03 被阅读0次

    项目中用到某些控件背景需要服务器控制返回值为字符串类似#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;
        }
    }
    

    相关文章

      网友评论

        本文标题:将蓝湖取值#1B9E5CFF(HEXA)转为RGBA颜色值

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