IOS假设将一个十六进制的color转换成UIColor,非常有用
2024-09-21 20:04:53
UI给开发的效果图非常多时候标注着十六进制的Color,而程序中用到的往往是UIColor能够用例如以下方法去转换:
(UIColor *)RGBColorFromHexString:(NSString *)color alpha:(float)alpha { //color的值相似#fffeee,alpha的值相似1.0为透明度
int nums[6] = {0};
for (int i = 1; i < [color length]; i++) {
int temp = [color characterAtIndex:i];
if (temp >= '0' && temp <= '9') {
nums[i-1] = [color characterAtIndex:i] - '0';
}else if(temp >= 'A' && temp <= 'F') {
nums[i-1] = [color characterAtIndex:i] - 'A' + 10;
}else if(temp >= 'a' && temp <= 'f') {
nums[i-1] = [color characterAtIndex:i] - 'a' + 10;
}else {
return [UIColor whiteColor];
} }
float rValue = (nums[0] * 16 + nums[1]) / 255.0f;
float gValue = (nums[2] *16 + nums[3]) / 255.0f;
float bValue = (nums[4] *16 + nums[5]) / 255.0f;
UIColor *rgbColor = [UIColor colorWithRed:rValue green:gValue blue:bValue alpha:alpha];
return rgbColor;
}
最新文章
- redis学习笔记
- Android开发之MVP模式的使用
- [leetcode] 题型整理之二叉树
- linux basis --- common commands
- JQuery 动画及一些小知识点
- Error LNK2019: unresolved external symbol ";char * __stdcall _com_util::ConvertBSTRToString(wchar_t *)";
- 【转】linux下awk内置函数的使用(split/substr/length)
- iOS 开发-- Runtime 1小时入门教程
- 非常实用的Android Studio快捷键
- iOS tableview和 Collection复用机制
- IDEA SpringBoot 打包(jar)
- 【python35小工具】b站弹幕保存
- static类型的变量
- day34 并发编程之生产者消费者模型 队列
- MySQL_插入更新 ON DUPLICATE KEY UPDATE
- swift - iOS10之后的加速器
- JavaScript -- Window-Scroll
- 洛谷P1445 樱花
- Cocos2d-x执行时错误:Cocos2d: Get data from file(xxx.xxx) failed!
- LeetCode: Insertion Sort List 解题报告