storyboard/xib中设置颜色:
代码中用UIColor的类方法设置颜色
+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;
或者给UIColor添加个category:
/**
设置颜色
@param hexColorString # 格式 或者 0x 格式
@param alpha 透明度
@return 设置的颜色
*/
+ (UIColor *)colorWithHexString:(NSString *)hexColorString alpha:(CGFloat)alpha {
if ([hexColorString length] < 6) { //长度不合法
return [UIColor blackColor];
}
NSString *tempString = [hexColorString lowercaseString];
if ([tempString hasPrefix:@"0x"]) { //检查开头是0x
tempString = [tempString substringFromIndex:2];
} else if ([tempString hasPrefix:@"#"]) { //检查开头是#
tempString = [tempString substringFromIndex:1];
}
if ([tempString length] != 6) {
return [UIColor blackColor];
}
//分解三种颜色的值
NSRange range = NSMakeRange(0, 2);
NSString *rString = [tempString substringWithRange:range];
range.location = 2;
NSString *gString = [tempString substringWithRange:range];
range.location = 4;
NSString *bString = [tempString substringWithRange:range];
//取三种颜色值
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];
}
通过storyboard/xib和代码设置的颜色色差很大,解决方法如下
方法1:storyboard/xib中直接改成device RGB
方法2:
如果你的storyboard/xib很多,每个都去改动要用很多时间,用
Xcode-Color-Fixer
但是Xcode-Color-Fixer认为storyboard/xib中RGB的默认设置都是sRGB,我在检查自己项目时发现有些默认还有generic RGB等,所以一是在脚本中将generic RGB也替换,或者自己多检查一下~