最近因项目需要,在网上找了一些代码,整合了一下,实现的效果就是将10进制的RGB色值转换IOS用的UIColor,方法还有缺陷,有待改进

UIColor *getColorFromString(NSString *colorString)
{
int colorInt=[colorString intValue];
if(colorInt<)
return [UIColor whiteColor]; NSString *nLetterValue;
NSString *colorString16 =@"";
int ttmpig;
for (int i = ; i<; i++)
{
ttmpig=colorInt%;
colorInt=colorInt/;
switch (ttmpig)
{
case :
nLetterValue =@"A";break;
case :
nLetterValue =@"B";break;
case :
nLetterValue =@"C";break;
case :
nLetterValue =@"D";break;
case :
nLetterValue =@"E";break;
case :
nLetterValue =@"F";break;
default:nLetterValue=[[NSString alloc]initWithFormat:@"%i",ttmpig]; }
colorString16 = [nLetterValue stringByAppendingString:colorString16];
if (colorInt == )
break;
} colorString16 = [[colorString16 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString]; //去掉前后空格换行符 // strip 0X if it appears
if ([colorString16 hasPrefix:@"0X"])
colorString16 = [colorString16 substringFromIndex:];
if ([colorString16 hasPrefix:@"#"])
colorString16 = [colorString16 substringFromIndex:];
// String should be 6 or 8 characters
if ([colorString16 length] < )
{
int cc=-[colorString16 length];
for (int i=; i<cc; i++)
colorString16=[@"" stringByAppendingString:colorString16];
}
if ([colorString16 length] != )
return [UIColor whiteColor]; // Separate into r, g, b substrings
NSRange range;
range.location = ;
range.length = ;
NSString *bString = [colorString16 substringWithRange:range]; range.location = ;
NSString *gString = [colorString16 substringWithRange:range]; range.location = ;
NSString *rString = [colorString16 substringWithRange:range]; // Scan values
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r]; //扫描16进制到int
[[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];
}

IOS中将十进制色值转换成UIColor的更多相关文章

  1. IOS 十六进制字符串转换成UIColor

    /** * 十六进制转换成UIColor * * @param stringToConvert 十六进制字符串 * * @return UIColor */ +(UIColor *) hexStrin ...

  2. ios 把毫秒值转换成日期 NSDate

    ios 把毫秒值转换成日期 (比较好用) 1343359790000 这是毫秒值------最佳解决方案-------------------- long long time=134335979000 ...

  3. 在MyEclipse中将Java Project转换成Web Project

    在MyEclipse中将Java Project转换成Web Project 此添加方法是针对MyEclipse中添加的: 编辑工程的.project文件: 添加 <nature>com. ...

  4. 十进制浮点数转换成IEEE754标准的32浮点数的二进制格式

    参考: http://jimmygod.blog.163.com/blog/static/43511339200792605627411/ http://blog.csdn.net/archersab ...

  5. 转载:在Excel中将数据库字段转换成驼峰式

    转载地址 在Excel中将数据库字段转换成驼峰式 1.将数据库字段复制到Excel表格第一列: 2.在第二列顶部输入=PROPER(A1)命令: 3.在第三列顶部输入=SUBSTITUTE(B1,&q ...

  6. JavaScript 将十进制数转换成格式类似于 0x000100 或 #000100 的十六进制数

    将十进制数转换成格式类似于 0x000100 或 #000100 的十六进制数 1 <!DOCTYPE html> 2 <html> 3 <head> 4 < ...

  7. 在js中将map对象转换成json 和 js对cookie的操作

    在js中将map对象转换成json //msp转objectlet obj= Object.create(null); for (let[k,v] of map) { obj[k] = v; }//o ...

  8. 将十六进制色值转换成Color

    在给Background赋值时,除了自带的Red,Blue,Black等,可以通过以下方法赋予其他颜色. 主要是将Hex转换成ARGB(A:alpha,表示透明度.R:Red.G:Green.B:Bl ...

  9. 16进制色值转换成RGB

    #51147f  转换成RGB ,5*16+1 ,1*16+4,7*16+15 #A9A9A9 转换成RGB ,A*16+9 ,A*16+9,A*16+9

随机推荐

  1. debian非正常关机进不了图形界面的解决方法

    昨天调试一个程序的时候,把界面设置成了POPUP方式,结果触发断点的时候,界面不能最小化,程序就死到那了,动不了,没办法只好按电源了,结果启动的时候提示 An automatic file syste ...

  2. [转]div里table居中的问题 Div与body顶部间隙

    本文转自:http://www.cnblogs.com/jinhui/archive/2008/09/24/1297729.html 将div的text-align设为center,然后将table的 ...

  3. 【总结】教你怎么将centos7打造成桌面系统

    http://tieba.baidu.com/p/3379447850 centos吧

  4. Linux下mplayer源代码安装方法

    http://www.linuxidc.com/Linux/2007-08/6820.htm 1下载安装mplayer需要的各种软件 去这里下载http://www.mplayerhq.hu/MPla ...

  5. Ubuntu lnmp安装记录

    参考了: Ubuntu 14.04 LTS 安装 LNMP Nginx\PHP5 (PHP-FPM)\MySQL 在访问 info.php的时候,我显示的是502错误.后面我参考了: Ubuntu15 ...

  6. android之硬件访问服务框架

    一.硬件接口描述文件aidl 新增\frameworks\base\core\java\android\os\ILedService.aidl 二.mk编译脚本 修改vi frameworks/bas ...

  7. (转)css3前缀

    CSS3的前缀是一个浏览器生产商经常使用的一种方式.它暗示该CSS属性或规则尚未成为W3C标准的一部分.看看都有哪些前缀: -webkit(chrome) -moz(firefox) -ms(ie) ...

  8. SonarQube(5.0.1) 环境的安装配置

    SonarQube 安装步骤 确定 JDK 和 MySQL 已经成功安装. 下载 SonarQube 及工具 SonarQube Runner,下载地址:http://www.sonarqube.or ...

  9. 在DataTable中添加行和列数据

    DataRow newRow = dtResult.NewRow(); newRow["ProName"] = "名字"; newRow["ProPr ...

  10. 关于sql row_number,rank,dense_rank,ntile函数

    row_number排序最好用它,它依次排名,不出现相同名次,如:1,2,3,4,5 rank出现相同排名,且跳过相同的排名号排下一名,如:1,1,3,4,5, dense_rank出现相同排名,不跳 ...