如何将 UIColor 转换为十六进制(网页颜色文本字符串)?
有没有一种简单的方法可以将 UIColor
转换为十六进制值?
或者我们是否必须使用 CGColorGetComponents 获取 RGB 分量,然后从那里计算出来?
例如 CGColorGetComponents(color.CGColor)[0] * 256 ?
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?
e.g CGColorGetComponents(color.CGColor)[0] * 256
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我还必须将 UIColor 转换为其十六进制组件。
正如 lewiguez 已经指出的那样,github 上有一个非常好的类别可以完成所有这些工作。
但因为我想了解它是如何完成的,所以我自己制作了 RGB 颜色的简单实现。
欢迎所有反馈!
I also had to convert a UIColor to its hex components.
As already pointed out by lewiguez there is a very good category at github that does all that stuff.
But because I wanted to learn how it is done I made my own simple implementation for RGB colours.
All feedback is welcome!
我会考虑使用 Erica Sadun 的 UIColor 类别。它包含许多免费功能,包括十六进制表示。它非常容易使用,只需将其添加到您使用它的任何类标头中,或者将其添加到预编译标头中以获得最终的灵活性。
如果您要添加到预编译标头,请执行类似于以下操作:
然后您可以像这样使用它
NSLog(@"%@", [myColor hexStringFromColor]);
GitHub 链接到 UIColor 类别:https://github.com/erica/uicolor-utilities
ArsTechnica 文章有关它: http://arstechnica.com/apple/guides /2009/02/iphone-development-accessing-uicolor-components.ars
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:
Then You can use it like so
NSLog(@"%@", [myColor hexStringFromColor]);
GitHub link to the UIColor category: https://github.com/erica/uicolor-utilities
ArsTechnica article about it: http://arstechnica.com/apple/guides/2009/02/iphone-development-accessing-uicolor-components.ars
除了广泛使用的基于字符串的解决方案之外,这里还有一个基于十六进制(整数)的解决方案。用法:
你会得到“color 120aef”。我会将这些代码放在 https://github.com/superarts/LCategory 中,或者您可以复制-也粘贴到您自己的代码库中:
Other than the widely used string based solution, here's a hex (integer) based solution. Usage:
And you'll get "color 120aef". I'll put these code in https://github.com/superarts/LCategory, or you can copy-paste into your own code bank too:
据我所知,没有任何内置的解决方案。
但请注意,您应该乘以
255
而不是256
。获取十六进制表示是一项简单的任务,因此手动构建字符串不会遇到很多麻烦。
There isn't, as far as I know, any built in solution.
Note however, that you should multiply by
255
instead of256
.Getting an hexadecimal representation is an easy task, so you won't have many troubles to build the string manually.
这就是我使用 Swift 3 所做的:
或者你可以将其设为一个方法。我只是喜欢
color.hex
与color.hex()
的外观。This is how I do did it with Swift 3:
Or you can make it a method. I just like how
color.hex
looks vscolor.hex()
.这是一个 Swift 5 解决方案,作为 UIColor 的扩展,将 UIColor 对象转换为 RGB (#RRGGBB) 形式或 RGBA (#RRGGBBAA) 格式的十六进制颜色字符串,也可以使用任一格式并创建 UIColor,并可选择提供带有 #RRGGBB 形式的 alpha 参数作为 alpha 的非默认值 1.0。
我从 @boidkan 的答案开始,然后在 SO 上搜索其他解决方案,然后将它们细化和优化为我可以接受的解决方案,并在操场上测试它们。
Here's a Swift 5 solution, as extensions to UIColor, to convert UIColor object to a hex color string of RGB (#RRGGBB) form or RGBA (#RRGGBBAA) format ,and also use either format and create a UIColor, with option to provide an alpha argument with #RRGGBB form as non-default value of 1.0 for alpha.
I started with @boidkan's answer then searched around for other solutions on S.O. and then refined and optimized them to a solution I could live with, and tested them in playground.