zoukankan      html  css  js  c++  java
  • 16进制颜色字符串转为UIColor

    //16进制颜色(html颜色值)字符串转为UIColor
    +(UIColor *) hexStringToColor: (NSString *) stringToConvert
    {
         NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
         // String should be 6 or 8 characters
     
         if ([cString length] < 6) return [UIColor blackColor];
         // strip 0X if it appears
         if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
         if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
         if ([cString length] != 6) return [UIColor blackColor];
     
         // Separate into r, g, b substrings
     
         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];
         // Scan values
         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:1.0f];
    }

  • 相关阅读:
    Java核心技术Java程序设计
    Mac下查看 Java 安装目录位置和安装数量
    Intellij IDEA快捷键与使用小技巧
    Java 8 新特性 用 Collectors 对 List 去重
    onInterceptTouchEvent()与onTouchEvent()的机制
    Android 开发之多线程处理、Handler
    安卓中使用XmlPullParser解析xml文件
    监控部署nagios+snmp
    阿里RDS数据库 全量备份恢复到本地MYSQL
    20120412
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/6899561.html
Copyright © 2011-2022 走看看