zoukankan      html  css  js  c++  java
  • iOS UIColor RGB HEX

    +(UIColor *)colorWithR:(CGFloat)r g:(CGFloat)g b:(CGFloat)b a:(CGFloat)a{
    
        return [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a/100.0f];
    }
    
    + (UIColor *)colorWithHexString:(NSString *)colorStr alpha:(CGFloat)alpha {
        
        //移除前缀
        if ([colorStr hasPrefix:@"0X"] || [colorStr hasPrefix:@"0x"]) {
            colorStr = [colorStr substringFromIndex:2];
        }
        
        if ([colorStr hasPrefix:@"#"]) {
            colorStr = [colorStr substringFromIndex:1];
        }
        
        //判断长度
        if (colorStr.length != 6) {
            return [UIColor clearColor];
        }
        
        //提取值
        NSRange range;
        range.length = 2;
        //r
        range.location = 0;
        NSString *rStr = [colorStr substringWithRange:range];
        //g
        range.location = 2;
        NSString *gStr = [colorStr substringWithRange:range];
        //b
        range.location = 4;
        NSString *bStr = [colorStr substringWithRange:range];
        
        //转换值
        unsigned int r, g, b;
        [[NSScanner scannerWithString:rStr] scanHexInt:&r];
        [[NSScanner scannerWithString:gStr] scanHexInt:&g];
        [[NSScanner scannerWithString:bStr] scanHexInt:&b];
        
        return [UIColor colorWithRed:(r/255.0f) green:(g/255.0f) blue:(b/255.0f) alpha:alpha];
    }
  • 相关阅读:
    正则表达式 (记录中...)
    css 坑记
    WebApi 中使用 Token
    WebApi 中使用 Session
    微信小程序 入门
    .net EF监控 MiniProfiler
    css布局
    移动端1像素边框问题
    移动端页面自适应解决方案:rem 布局篇
    js重点知识总结
  • 原文地址:https://www.cnblogs.com/ficow/p/5782991.html
Copyright © 2011-2022 走看看