iOS UIColor 常用 Category(类目)分享

西门桃桃 2020-08-01 AM 1875℃ 0条

1、16进制色值字符串转UIColor:

//使用时直接使用宏定义即可,简单方便
#define HexColor(s) [UIColor colorFromHex:s]
#define HexAlphaColor(s,a) [UIColor colorFromHex:s alpha:a]
#define HexStringColor(s) [UIColor colorFromHexString:s]
#define HexStringAlphaColor(s,a) [UIColor colorFromHexString:s alpha:a]

#define RGB(r,g,b) ([UIColor colorWithRed:(float)r/255.0f green:(float)g/255.0f blue:(float)b/255.0f alpha:1])
#define RGBA(r,g,b,a) ([UIColor colorWithRed:(float)r/255.0f green:(float)g/255.0f blue:(float)b/255.0f alpha:a])
#define RefRGBA(r,g,b,a) ([RGBA(r,g,b,a) CGColor])


+(UIColor *) colorFromHex:(NSUInteger)hexValue {
    UIColor *color = nil;
    if ((hexValue >> 24) > 0) //AARRGGBB
    {
        color = [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0
                                green:((float)((hexValue & 0xFF00) >> 8))/255.0
                                 blue:((float)(hexValue & 0xFF))/255.0
                                alpha:((float)((hexValue & 0xFF000000) >> 24))/255.0];
    }
    else //RRGGBB
    {
        color = [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0
                                green:((float)((hexValue & 0xFF00) >> 8))/255.0
                                 blue:((float)(hexValue & 0xFF))/255.0
                                alpha:1.0];
    }
    return color;
}

+(UIColor *) colorFromHex:(NSUInteger)hex alpha:(CGFloat)alpha {
    return [[self colorFromHex:hex] colorWithAlphaComponent:alpha];
}

/**
 *  get UIClolor from hex string
 */
+(UIColor *)colorFromHexString:(NSString *)hex{
    return [self colorFromHexString:hex alpha:1];
}

+(UIColor *) colorFromHexString:(NSString*)hex alpha:(CGFloat)alpha {
    hex= [[hex uppercaseString] substringFromIndex:1];
    CGFloat valueArray[3];
    NSArray *strArray=[NSArray arrayWithObjects:[hex substringWithRange:NSMakeRange(0, 2)],[hex substringWithRange:NSMakeRange(2, 2)],[hex substringWithRange:NSMakeRange(4, 2)] ,nil];
    for( int i=0;i<strArray.count;i++){
        hex=strArray[i];
        CGFloat value=([hex characterAtIndex:0]>'9'?[hex characterAtIndex:0]-'A'+10:[hex characterAtIndex:0]-'0')*16.0f+([hex characterAtIndex:1]>'9'?[hex characterAtIndex:1]-'A'+10:[hex characterAtIndex:1]-'0');
        valueArray[i]=value;
    }
    return [UIColor colorWithRed:valueArray[0]/255 green:valueArray[1]/255 blue:valueArray[2]/255 alpha:alpha];
}

非特殊说明,本博所有文章均为博主原创。

评论啦~