@interface UIColor (Hex)
/**
* 16进制字符串转化为颜色
* @param hexStr 16进制字符串 支持@"0x..." @"0X..." @"#..." @"..."
* @return UIColor
*/
+ (UIColor *)colorWithHexString:(NSString *)hexStr;
/**
* 16进制字符串转化为颜色
* @param hexStr 16进制字符串
* @param alpha 透明度
* @return UIColor
*/
+ (UIColor *)colorWithHexString:(NSString *)hexStr alpha:(NSInteger)alpha;
@end
/**
* 16进制色值转化颜色
* @param hexInteger 16进制色值
* @return UIColor
*/
+ (UIColor *)colorWithHex:(NSInteger)hexInteger;
/**
* 16进制色值转化颜色
* @param hexInteger 16进制色值
* @param alpha 透明度
* @return UIColor
*/
+ (UIColor *)colorWithHex:(NSInteger)hexInteger alpha:(NSInteger)alpha;
@implementation UIColor (Hex)
/**
* 16进制字符串转化为颜色
*/
+ (UIColor *)colorWithHexString:(NSString *)hexStr {
//字符串去除两端空格,变大写
NSString *colorStr = [[hexStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
//至少6位
if (colorStr.length < 6) {
return [UIColor clearColor];
}
//进一步判断,处理
if ([colorStr hasPrefix:@"0X"]) {
colorStr = [colorStr substringFromIndex:2];
}
if ([colorStr hasPrefix:@"#"]) {
colorStr = [colorStr substringFromIndex:1];
}
if (colorStr.length < 6) {
return [UIColor clearColor];
}
NSRange range;
range.length = 2;
//R
range.location = 0;
NSString *rString = [colorStr substringWithRange:range];
//G
range.location = 2;
NSString *gString = [colorStr substringWithRange:range];
//B
range.location = 4;
NSString *bString = [colorStr substringWithRange:range];
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:bString] scanHexInt:&g];
[[NSScanner scannerWithString:gString] scanHexInt:&b];
return [UIColor colorWithRed:((float)r / 255.0) green:((float)g / 255.0) blue:((float)b / 255.0) alpha:1];
}
+ (UIColor *)colorWithHexString:(NSString *)hexStr alpha:(NSInteger)alpha {
//字符串去除两端空格,变大写
NSString *colorStr = [[hexStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
//至少6位
if (colorStr.length < 6) {
return [UIColor clearColor];
}
//进一步判断,处理
if ([colorStr hasPrefix:@"0X"]) {
colorStr = [colorStr substringFromIndex:2];
}
if ([colorStr hasPrefix:@"#"]) {
colorStr = [colorStr substringFromIndex:1];
}
if (colorStr.length < 6) {
return [UIColor clearColor];
}
NSRange range;
range.length = 2;
//R
range.location = 0;
NSString *rString = [colorStr substringWithRange:range];
//G
range.location = 2;
NSString *gString = [colorStr substringWithRange:range];
//B
range.location = 4;
NSString *bString = [colorStr substringWithRange:range];
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:bString] scanHexInt:&g];
[[NSScanner scannerWithString:gString] scanHexInt:&b];
return [UIColor colorWithRed:((float)r / 255.0) green:((float)g / 255.0) blue:((float)b / 255.0) alpha:alpha];
}
/**
* 16进制色值转化颜色
*/
+ (UIColor *)colorWithHex:(NSInteger)hexInteger {
return [UIColor colorWithRed:((float)((hexInteger & 0xFF0000) >> 16)) / 255.0
green:((float)((hexInteger & 0xFF00) >> 8)) / 255.0
blue:((float)(hexInteger & 0xFF)) alpha:1];
}
+ (UIColor *)colorWithHex:(NSInteger)hexInteger alpha:(NSInteger)alpha {
return [UIColor colorWithRed:((float)((hexInteger & 0xFF0000) >> 16)) / 255.0
green:((float)((hexInteger & 0xFF00) >> 8)) / 255.0
blue:((float)(hexInteger & 0xFF)) alpha:alpha];
}
@end
(1)宏定义RGB_COLOR和RGBA_COLOR可以设置颜色
(2)UIColor+Hex扩展可以设置颜色
(3)导航栏上面的BarButtonItem怎么设置为Button
(4)Button一些常用和不常用的属性设置