UIColor支持十六进制,生成随机颜色,根据一个颜色生成一张图片

UIColor+Hex.h

#import   
  
#define RGBA_COLOR(R, G, B, A) [UIColor colorWithRed:((R) / 255.0f) green:((G) / 255.0f) blue:((B) / 255.0f) alpha:A]  
#define RGB_COLOR(R,G,B) [UIColor colorWithRed:((R) / 255.0f) green: ((G) / 255.0f) blue:((B) / 255.0f) alpha: 1.0f]
//随机生成颜色
#define RandomColor random(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))

@interface UIColor (Hex)  
  
// 从十六进制字符串获取颜色,  
// color:支持@“#123456”、 @“0X123456”、 @“123456”三种格式  
+ (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha;  
+ (UIColor *)colorWithHexString:(NSString *)color;
//根据UIColor一个颜色生成一张图片
+ (UIImage *)imageWithColor:(UIColor *)color;

@end

UIColor+Hex.m

#import "UIColor+Hex.h"  
  
@implementation UIColor (Hex)  
  
+ (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha {  
    // 删除字符串中的空格  
    NSString * colorStr = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];  
      
    // String should be 6 or 8 characters  
    if ([colorStr length] < 6) {  
        return [UIColor clearColor];  
    }  
      
    // strip 0X if it appears  
    // 如果是0x开头的,那么截取字符串,字符串从索引为2的位置开始,一直到末尾  
    if ([colorStr hasPrefix:@"0X"]) {  
        colorStr = [colorStr substringFromIndex:2];  
    }  
      
    // 如果是#开头的,那么截取字符串,字符串从索引为1的位置开始,一直到末尾  
    if ([colorStr hasPrefix:@"#"]) {  
        colorStr = [colorStr substringFromIndex:1];  
    }  
      
    // 除去所有开头字符后 再判断字符串长度  
    if ([colorStr length] != 6) {  
        return [UIColor clearColor];  
    }  
      
    // Separate into r, g, b substrings  
    NSRange range;  
    range.location = 0;  
    range.length = 2;  
    //red  
    NSString * redStr = [colorStr substringWithRange:range];  
    //green  
    range.location = 2;  
    NSString * greenStr = [colorStr substringWithRange:range];  
    //blue  
    range.location = 4;  
    NSString * blueStr = [colorStr substringWithRange:range];  
      
    // Scan values 将十六进制转换成二进制  
    unsigned int r, g, b;  
    [[NSScanner scannerWithString:redStr] scanHexInt:&r];  
    [[NSScanner scannerWithString:greenStr] scanHexInt:&g];  
    [[NSScanner scannerWithString:blueStr] scanHexInt:&b];  
    return [UIColor colorWithRed:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:alpha];  
}  
 
+ (UIColor *)colorWithHexString:(NSString *)color {
    return [self colorWithHexString:color alpha:1.0f];
}

+ (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); //宽高 1.0只要有值就够了
    UIGraphicsBeginImageContext(rect.size); //在这个范围内开启一段上下文
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);//在这段上下文中获取到颜色UIColor
    CGContextFillRect(context, rect);//用这个颜色填充这个上下文

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();//从这段上下文中获取Image属性,,,结束
    UIGraphicsEndImageContext();

    return image;
   }

@end

参考文章:
OC_UIColor 类扩展支持十六进制
iOS开发小方法:根据UIColor一个颜色生成一张图片
iOS -UIColor随机生成颜色的方法

你可能感兴趣的:(UIColor支持十六进制,生成随机颜色,根据一个颜色生成一张图片)