iOS 11 这个特性你知道吗?
2017-09-20 本文已影响239人
Lefe
适配 iOS 11 时意外发现个 New Color Set ,仔细研究了下,发现比较爽。它集中管理项目中的颜色,项目中有多少颜色一目了然。
![](https://img.haomeiwen.com/i1664496/64ad1311e8de7814.png)
使用的时候,直接使用:
[UIColor colorNamed:name];
但是这个方法只有在 iOS 11 以上系统有效,我们可以自己实现一个方法,或者把系统的方法替换掉。
@implementation UIColor (main)
+ (UIColor *)mtColorNamed:(NSString *)name
{
if (name.length == 0) {
return [UIColor clearColor];
}
NSString *cString = [[name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
if (cString.length != 6) {
return [UIColor clearColor];
}
if (@available(iOS 11.0, *)) {
return [UIColor colorNamed:name];
} else {
return [self mtColorWithHexString:name];
}
}
+ (UIColor *)mtColorWithHexString:(NSString *)color
{
unsigned int r, g, b;
[[NSScanner scannerWithString:[color substringWithRange:NSMakeRange(0, 2)]] scanHexInt:&r];
[[NSScanner scannerWithString:[color substringWithRange:NSMakeRange(2, 2)]] scanHexInt:&g];
[[NSScanner scannerWithString:[color substringWithRange:NSMakeRange(4, 2)]] scanHexInt:&b];
return [UIColor colorWithRed:((CGFloat) r / 255.0f) green:((CGFloat) g / 255.0f) blue:((CGFloat) b / 255.0f) alpha:1.0f];
}
@end
使用时,直接调用我们自定义的方法即可:
static NSString* const k50E3C2Color = @"50E3C2";
static NSString* const k10AEFFColor = @"10AEFF";
- (void)viewDidLoad {
[super viewDidLoad];
_label = [[UILabel alloc] initWithFrame:CGRectMake(40, 100, 100, 50)];
_label.text = k50E3C2Color;
_label.textAlignment = NSTextAlignmentCenter;
_label.textColor = [UIColor mtColorNamed:k10AEFFColor];
_label.backgroundColor = [UIColor mtColorNamed:k50E3C2Color];
[self.view addSubview:_label];
}
![](https://img.haomeiwen.com/i1664496/65035cf2b74a7123.png)
推荐阅读
===== 我是有底线的 ======
喜欢我的文章,欢迎关注我的新浪微博 Lefe_x,我会不定期的分享一些开发技巧
![](https://img.haomeiwen.com/i1664496/e409f16579811101.jpg)