iOS-蓝湖Hex十六进制取色值封装

iOS-蓝湖Hex十六进制取色值封装

  • 前言
    • 上代码
      • 只有一个UIColor的颜色分类 - UIColor+ColorHex
      • 宏的封装 - 点睛之笔
    • 使用

前言

  • 在我们取蓝湖色值时总是被颜色的取值耽误时间,可不可以直接取蓝湖上的Hex的值,进行操作即可
    iOS-蓝湖Hex十六进制取色值封装_第1张图片

上代码

只有一个UIColor的颜色分类 - UIColor+ColorHex

  • UIColor+ColorHex.h
#import 
#import 

@interface UIColor(ColorHex)

+ (UIColor *)ly_colorWithHexString:(NSString *)hexString;
+ (UIColor *)ly_colorWithHexString:(NSString *)hexString withAlpha:(float)alp;

@end
  • UIColor+ColorHex.m
#import "UIColor+ColorHex.h"

@implementation UIColor(ColorHex)

#pragma mark - 颜色值十六进制 转换  #ffffff -> uicolor

+ (UIColor *)ly_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 ly_colorComponentFrom:colorString start:0 length:1];
            green = [self ly_colorComponentFrom:colorString start:1 length:1];
            blue  = [self ly_colorComponentFrom:colorString start:2 length:1];
            break;
        case 4: // #ARGB
            alpha = [self ly_colorComponentFrom:colorString start:0 length:1];
            red   = [self ly_colorComponentFrom:colorString start:1 length:1];
            green = [self ly_colorComponentFrom:colorString start:2 length:1];
            blue  = [self ly_colorComponentFrom:colorString start:3 length:1];
            break;
        case 6: // #RRGGBB
            alpha = 1.0f;
            red   = [self ly_colorComponentFrom:colorString start:0 length:2];
            green = [self ly_colorComponentFrom:colorString start:2 length:2];
            blue  = [self ly_colorComponentFrom:colorString start:4 length:2];
            break;
        case 8: // #AARRGGBB
            alpha = [self ly_colorComponentFrom:colorString start:0 length:2];
            red   = [self ly_colorComponentFrom:colorString start:2 length:2];
            green = [self ly_colorComponentFrom:colorString start:4 length:2];
            blue  = [self ly_colorComponentFrom:colorString start:6 length:2];
            break;
        default:
            NSLog(@"色值有问题,被改为默认白色了,具体代码全局搜索这条log输出文字");
            return [UIColor whiteColor];
            //[NSException raise:@"Invalid color value" format: @"Color value %@ is invalid.  It should be a hex value of the form #RBG, #ARGB, #RRGGBB, or #AARRGGBB", hexString];
            break;
    }
    return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
}

+ (UIColor *)ly_colorWithHexString:(NSString *)hexString withAlpha:(float)alp {
    NSString *colorString = [[hexString stringByReplacingOccurrencesOfString:@"#" withString:@""] uppercaseString];
    CGFloat alpha, red, blue, green;
    switch ([colorString length]) {
        case 3: // #RGB
            alpha = alp;
            red   = [self ly_colorComponentFrom:colorString start:0 length:1];
            green = [self ly_colorComponentFrom:colorString start:1 length:1];
            blue  = [self ly_colorComponentFrom:colorString start:2 length:1];
            break;
        case 4: // #ARGB
            alpha = [self ly_colorComponentFrom:colorString start:0 length:1];
            red   = [self ly_colorComponentFrom:colorString start:1 length:1];
            green = [self ly_colorComponentFrom:colorString start:2 length:1];
            blue  = [self ly_colorComponentFrom:colorString start:3 length:1];
            break;
        case 6: // #RRGGBB
            alpha = alp;
            red   = [self ly_colorComponentFrom:colorString start:0 length:2];
            green = [self ly_colorComponentFrom:colorString start:2 length:2];
            blue  = [self ly_colorComponentFrom:colorString start:4 length:2];
            break;
        case 8: // #AARRGGBB
            alpha = [self ly_colorComponentFrom:colorString start:0 length:2];
            red   = [self ly_colorComponentFrom:colorString start:2 length:2];
            green = [self ly_colorComponentFrom:colorString start:4 length:2];
            blue  = [self ly_colorComponentFrom:colorString start:6 length:2];
            break;
        default:
            NSLog(@"色值有问题,被改为默认白色了,具体代码全局搜索这条log输出文字");
            return [UIColor whiteColor];
            //[NSException raise:@"Invalid color value" format: @"Color value %@ is invalid.  It should be a hex value of the form #RBG, #ARGB, #RRGGBB, or #AARRGGBB", hexString];
            break;
    }
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

+ (CGFloat)ly_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;
}

@end

宏的封装 - 点睛之笔

  • ViewController.m 中引入头文件 UIColor+ColorHex.h
#import "UIColor+ColorHex.h"
  • 定义宏
    • CFSTR() :C语言的接口,直接将 #333333 十六进制,转换为字符串函数
    • (__bridge NSString *) : 强转成OC对象
//直接取值
#define LY_Color_Hex(hex) [UIColor ly_colorWithHexString:((__bridge NSString *)CFSTR(#hex))]
//直接取值 + 透明度
#define LY_Color_HexAlpha(hex,alp) [UIColor ly_colorWithHexString:((__bridge NSString *)CFSTR(#hex)) withAlpha:alp]

使用

  • LY_Color_Hex(#333333)

你可能感兴趣的:(ios)