zoukankan      html  css  js  c++  java
  • 十六进制string转换UIColor -备用

    - (UIColor *) colorWithHexString: (NSString *) stringToConvert

    {

        NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];//字符串处理

        //例子,stringToConvert #ffffff

        if ([cString length] < 6)

            return DEFAULT_VOID_COLOR;//如果非十六进制,返回白色

        if ([cString hasPrefix:@"#"])

            cString = [cString substringFromIndex:1];//去掉头

        if ([cString length] != 6)//去头非十六进制,返回白色

            return DEFAULT_VOID_COLOR;

        //分别取RGB的值

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

        

        unsigned int r, g, b;

    //NSScanner把扫描出的制定的字符串转换成Int类型

        [[NSScanner scannerWithString:rString] scanHexInt:&r];

        [[NSScanner scannerWithString:gString] scanHexInt:&g];

        [[NSScanner scannerWithString:bString] scanHexInt:&b];

        //转换为UIColor

        return [UIColor colorWithRed:((float) r / 255.0f)

                               green:((float) g / 255.0f)

                                blue:((float) b / 255.0f)

                               alpha:1.0f];

    }

  • 相关阅读:
    web.xml
    web.xml hello1代码分析
    annotation
    injection
    container
    build tool
    version control
    url与uri的区别
    函数式语言
    http协议解析过程
  • 原文地址:https://www.cnblogs.com/isItOk/p/5419399.html
Copyright © 2011-2022 走看看