如何从十六进制字符串创建UIColor? [英] How can I create a UIColor from a hex string?

查看:126
本文介绍了如何从十六进制字符串创建UIColor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用十六进制字符串格式(如#00FF00

创建 UIColor

解决方案

我发现最简单的方法是使用宏。

  #define UIColorFromRGB(rgbValue)[UIColor colorWithRed:((float )(255)blue((float(rgbValue& 0xFF0000)> 16))/ 255.0 green:((float)((rgbValue& 0xFF00)> ))/ 255.0 alpha:1.0] 

带十六进制值的uicolor宏



这个代码的格式化版本:

  #define UIColorFromRGB(rgbValue)\ 
[UIColor colorWithRed:((float) ((rgbValue& 0xFFFFFF))> 16))/ 255.0 \
green:((float)((rgbValue& 0x00FF00)>> 8))/ 255.0 \
blue:((float)((rgbValue& 0x0000FF)> 0))/ 255.0 \
alpha:1.0]

用法:

  label.textColor = UIColorFromRGB(0xBC1128); 


How can I create a UIColor from a hexadecimal string format, such as #00FF00?

解决方案

I've found the simplest way to do this is with a macro. Just include it in your header and it's available throughout your project.

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

uicolor macro with hex values

Also formatted version of this code:

#define UIColorFromRGB(rgbValue) \
[UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
                green:((float)((rgbValue & 0x00FF00) >>  8))/255.0 \
                 blue:((float)((rgbValue & 0x0000FF) >>  0))/255.0 \
                alpha:1.0]

Usage:

label.textColor = UIColorFromRGB(0xBC1128);

这篇关于如何从十六进制字符串创建UIColor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆