美文网首页
iOS 常用RGB十六进制颜色转换方法

iOS 常用RGB十六进制颜色转换方法

作者: Levi段玉磊 | 来源:发表于2018-12-26 16:14 被阅读0次

    iOS 常用RGB十六进制颜色转换方法

    需求

    正常美术出图都是给你十六进制编码,然后让我们在Xcode中,对颜色进行赋值,由于iOS的UIColor并未提供方法直接使用十六进制编码对设置颜色的场景进行赋值,所以我们需要造个小轮子。

    实现方式

    通过分类的方法实现:

    UIColor+Addition.h

    @interface UIColor (Addition)
    
    //设置RGB颜色
    + (UIColor *)red:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue alpha:(CGFloat)alpha;
    //将颜色转换成RGB
    + (NSArray *)convertColorToRGB:(UIColor *)color;
    //设置十六进制颜色
    + (UIColor *)colorWithHex:(NSInteger)hex;
    + (UIColor *)colorWithHexString:(NSString *)hexString;
    + (UIColor *)colorWithHexString:(NSString *)hexString alpha:(CGFloat)alpha
    
    @end
    

    UIColor+Addition.m

    #import "UIColor+Addition.h"
    
    @implementation UIColor (Addition)
    
    + (UIColor *)red:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue alpha:(CGFloat)alpha {
        return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha];
    }
    
    + (NSArray *)convertColorToRGB:(UIColor *)color {
        NSInteger numComponents = CGColorGetNumberOfComponents(color.CGColor);
        NSArray *array = nil;
        if (numComponents == 4) {
            const CGFloat *components = CGColorGetComponents(color.CGColor);
            array = @[@((int)(components[0] * 255)),
                      @((int)(components[1] * 255)),
                      @((int)(components[2] * 255))];
        }
        return array;
    }
    
    + (UIColor *)colorWithHex:(NSInteger)hex {
        return [UIColor colorWithRed:((float)((hex & 0xff0000) >> 16))/255.0 green:((float)((hex & 0x00ff00) >> 8))/255.0 blue:((float)(hex & 0x0000ff))/255.0 alpha:1.0];
    }
    
    + (UIColor *)colorWithHexString:(NSString *)hexString
    {
        NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#" withString: @""] uppercaseString];
        CGFloat alpha, red, blue, green;
        switch ([colorString length]) {
                case 3: // #RGB
                alpha = 1.0f;
                red   = [self colorComponentFrom: colorString start: 0 length: 1];
                green = [self colorComponentFrom: colorString start: 1 length: 1];
                blue  = [self colorComponentFrom: colorString start: 2 length: 1];
                break;
                case 4: // #ARGB
                alpha = [self colorComponentFrom: colorString start: 0 length: 1];
                red   = [self colorComponentFrom: colorString start: 1 length: 1];
                green = [self colorComponentFrom: colorString start: 2 length: 1];
                blue  = [self colorComponentFrom: colorString start: 3 length: 1];
                break;
                case 6: // #RRGGBB
                alpha = 1.0f;
                red   = [self colorComponentFrom: colorString start: 0 length: 2];
                green = [self colorComponentFrom: colorString start: 2 length: 2];
                blue  = [self colorComponentFrom: colorString start: 4 length: 2];
                break;
                case 8: // #AARRGGBB
                alpha = [self colorComponentFrom: colorString start: 0 length: 2];
                red   = [self colorComponentFrom: colorString start: 2 length: 2];
                green = [self colorComponentFrom: colorString start: 4 length: 2];
                blue  = [self colorComponentFrom: colorString start: 6 length: 2];
                break;
            default:
                blue=0;
                green=0;
                red=0;
                alpha=0;
                break;
        }
        return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
    }
    
    + (CGFloat)colorComponentFrom:(NSString *)string start:(NSUInteger)start length:(NSUInteger) length
    {
        NSString *substring = [string substringWithRange: NSMakeRange(start, length)];
        NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat: @"%@%@", substring, substring];
        unsigned hexComponent;
        [[NSScanner scannerWithString: fullHex] scanHexInt: &hexComponent];
        return hexComponent / 255.0;
    }
    
    + (UIColor *)colorWithHexString:(NSString *)hexString alpha:(CGFloat)alpha
    {
        NSString *cString = [[hexString stringByTrimmingCharactersInSet:
                              [NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
        
        if ([cString length] < 6) {
            return [UIColor whiteColor];
        }
        
        if ([cString hasPrefix:@"#"]) {
            cString = [cString substringFromIndex:1];
        }
        
        if ([cString length] != 6) {
            return [UIColor whiteColor];
        }
        
        NSRange range;
        range.location = 0;
        range.length = 2;
        NSString *rString = [cString substringWithRange:range];
        
        range.location = 2;
        NSString *gString = [cString substringWithRange:range];
        
        range.location = 4;
        NSString *bString = [cString substringWithRange:range];
        
        unsigned int r, g, b;
        [[NSScanner scannerWithString:rString] scanHexInt:&r];
        [[NSScanner scannerWithString:gString] scanHexInt:&g];
        [[NSScanner scannerWithString:bString] scanHexInt:&b];
        
        return [UIColor colorWithRed:((float) r/255.0f)
                               green:((float) g/255.0f)
                                blue:((float) b/255.0f)
                               alpha:alpha];
    }
    

    思考与总结:

    需要记住的是:

    纯黑是 #000000
    纯白是 #FFFFFF

    相关文章

      网友评论

          本文标题:iOS 常用RGB十六进制颜色转换方法

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