美文网首页Swift开发iOS Developer
iOS 11 这个特性你知道吗?

iOS 11 这个特性你知道吗?

作者: Lefe | 来源:发表于2017-09-20 16:29 被阅读239次

    适配 iOS 11 时意外发现个 New Color Set ,仔细研究了下,发现比较爽。它集中管理项目中的颜色,项目中有多少颜色一目了然。

    color.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];
    }
    
    result.png

    推荐阅读

    iOS 11 适配看这篇还不够?

    ===== 我是有底线的 ======
    喜欢我的文章,欢迎关注我的新浪微博 Lefe_x,我会不定期的分享一些开发技巧

    相关文章

      网友评论

        本文标题:iOS 11 这个特性你知道吗?

        本文链接:https://www.haomeiwen.com/subject/nieosxtx.html