UIImage的Category总结

  • 经常在开发中给视图设置颜色,所以给UIColor提供扩展方法是必须的。我们来简单了解几种方便设置颜色的方法。此文会持续更新。。。

简单的宏定义

#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]

根据RGBHex 生成颜色类(透明度为1)

/**
 根据RGBHex 生成颜色类(透明度为1)
 如:000000
 */
 + (instancetype)colorWithRGBHexStr:(NSString*)str
{
    NSInteger hex = [str integerValue];
    int r = (hex >> 16) & 0xFF;
    int g = (hex >> 8) & 0xFF;
    int b = (hex)&0xFF;
    return [UIColor colorWithRed:r / 255.0f
                           green:g / 255.0f
                            blue:b / 255.0f
                           alpha:1.0f];
}

根据html对应的色值 生成颜色类(透明度为1)

/**
 根据html对应的色值 生成颜色类(透明度为1)
 如:#000000 或者000000
 */
 + (instancetype)colorWithHexString:(NSString*)stringToConvert
{
    NSString* hexString = [stringToConvert stringByReplacingOccurrencesOfString:@"#" withString:@""];
    NSScanner* scanner = [NSScanner scannerWithString:hexString];
    unsigned hexNum;
    if (![scanner scanHexInt:&hexNum]){
        return nil;
    }
    return colorWithRGBHex(hexNum);
//    或者
//    return [UIColor colorWithRGBHexStr:[NSString stringWithFormat:@"%u",hexNum]];
}

//内联函数
static inline UIColor* colorWithRGBHex(UInt32 hex){
    int r = (hex >> 16) & 0xFF;
    int g = (hex >> 8) & 0xFF;
    int b = (hex)&0xFF;
    return [UIColor colorWithRed:r / 255.0f
                           green:g / 255.0f
                            blue:b / 255.0f
                           alpha:1.0f];
}

根据html对应的色值 生成颜色类(透明度自定义)

/**
根据html对应的色值 生成颜色类(透明度自定义)
如:#000000 或者000000
*/
+ (instancetype)colorWithHexString:(NSString *)hexString
                          alpha:(float)alpha {

    NSString *cString = [[hexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];

    // String should be 6 or 8 characters
    if ([cString length] < 6) return nil;

    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
    if ([cString length] != 6) return nil;
    // Separate into r, g, b substrings
    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];

    // Scan values
    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];
}