iOS中十六进制的颜色转换为UIColor
2015-09-14 12:53
513 查看
直接贴代码如下:
+(UIColor*)getColor:(NSString *)color{ NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString]; // colorString should be 6 or 8 characters if ([cString length] < 6) { return [UIColor clearColor]; } // strip 0X /# if it appears if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2]; if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1]; // string should be 6 characters if ([cString length] != 6) return [UIColor clearColor]; // Separate into r, g, b substrings NSRange range; range.location = 0; range.length = 2; //r NSString *rString = [cString substringWithRange:range]; //g range.location = 2; NSString *gString = [cString substringWithRange:range]; //b 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]; CGFloat redFloat=(float) r / 255.0f; CGFloat greenFloat=(float) g / 255.0f; CGFloat blueFloat=(float) b / 255.0f; UIColor *blockColor=[UIColor colorWithRed:redFloat green:greenFloat blue:blueFloat alpha:1.0f]; return blockColor; }
相关文章推荐
- STL 之 stack&queue&priority_queue 栈,队列与优先队列
- Android UI 之 Tab类型界面总结
- UICollectionViewCell 多选知识点更新
- JAVA的String、StringBuilder和StringBuffer类的区别
- UITabBarControler解决旋转问题
- UITableView和侧滑返回冲突问题
- HDOJ 4424 Conquer a New Region
- 看unix高级编程(第三版)时遇到apue.h找不到的问题
- erl_stack_queue-队列求迷宫最短路径
- HiWork发布1.6.0版本——调整UI布局,新增消息搜索等多项功能
- erl_stack_queue-利用栈实现迷宫的求解
- How-to: write own Kafka Partitioner based on requirement
- Unix环境高级编程开篇-"apue.h"配置
- qt使用uic命令将后缀名为.ui的文件输出成.h文件方法
- UITextField使用总结
- UITableView+FDTemplateLayoutCell源码学习笔记
- js1:根据标签的Id获取value值
- iOS UI基础-6.0 UIActionSheet的使用
- Permutation sequence
- IOS UI学习 UISearchBar