UIColor 到十六进制(网页颜色) [英] UIColor to Hexadecimal (web color)
问题描述
是否有一种简单的方法可以将 UIColor
转换为十六进制值?
或者我们是否必须使用 CGColorGetComponents
获取 RGB 组件,然后从那里开始计算?
Is there an easy way to convert UIColor
to a hexadecimal value ?
Or do we have to get the RGB components with CGColorGetComponents
and then work it out from there?
例如 CGColorGetComponents(color.CGColor)[0] * 256
?
推荐答案
我会考虑使用 Erica Sadun 的 UIColor 类别.它包括许多免费功能,包括十六进制表示.它非常易于使用,只需将其添加到您使用它的任何类头中,或者将其添加到预编译头中以获得最大的灵活性.如果您要添加到预编译标头,请执行类似于以下操作:
I would consider using Erica Sadun's UIColor category. It includes a lot of functionality for free, including hex representations. It's pretty easy to use, just add it to whatever class header you're using it in or, add it to the pre-compiled header for ultimate flexibility. If you're adding to the pre-compiled header, do so similar to something like this:
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "UIColor-Expanded.h"
#endif
然后你可以这样使用它NSLog(@"%@", [myColor hexStringFromColor]);
指向 UIColor 类别的 GitHub 链接:https://github.com/erica/uicolor-utilities一个>
GitHub link to the UIColor category: https://github.com/erica/uicolor-utilities
ArsTechnica 关于它的文章:http:///arstechnica.com/apple/guides/2009/02/iphone-development-accessing-uicolor-components.ars
ArsTechnica article about it: http://arstechnica.com/apple/guides/2009/02/iphone-development-accessing-uicolor-components.ars
这篇关于UIColor 到十六进制(网页颜色)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!