如何将 HEX RGB 颜色代码转换为 UIColor?
我有一个像 #ffffff 这样的 RGB 十六进制代码作为 NSString,并且想要将其转换为 UIColor。有没有一种简单的方法可以做到这一点?
I have an RGB hex code like #ffffff as NSString and want to convert that into an UIColor. Is there a simple way to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
在我的一些代码中,我使用了 2 个不同的函数:
然后我这样使用它:
如果您更喜欢将其用作一个
UIColor
类别,那么只需更改几行即可:这将处理“#abc”、“#abcdef31”等字符串。
In some code of mine, I use 2 different functions:
And then I use it like this:
If you prefer to use this as a
UIColor
category, then it's just a matter of altering a few lines:This will handle strings like "#abc", "#abcdef31", etc.
如果您使用十六进制值..
If you are using Hex Values..
我一直在寻找一个简单的解决方案,并想出了这个(不完全是 Objective-C,但工作起来就像一个魅力):
I was looking for a simple solution and came up with this (not completely Objective-C, but works like a charm):
UIColor 有一个很好的类别,名为 "UIColor+Expanded" ,它有一个类方法用于获取来自 RGB 十六进制字符串的 UIColor:
使用起来很简单:
此外,它还为 UIColor 添加了许多其他可能有用的实用程序。更多信息请参阅本文。
There is a nice category for UIColor called "UIColor+Expanded" which has a class method for getting a UIColor from an RGB hex string:
It's simple to use:
Plus it adds a lot of other potentially useful utilities to UIColor. More info is available in this article.
很简单,只需访问此网站并输入您的十六进制值:http://www. corecoding.com/utilities/rgb-or-hex-to-float.php
Easy, Just go to this website and type in your hex value: http://www.corecoding.com/utilities/rgb-or-hex-to-float.php
对于格式 #123、123、#fff195、fff195
对于格式 0xfff195
for format #123, 123, #fff195, fff195
for format 0xfff195
我发现的最简单的方法: 十六进制到 UIColor 转换器
只需键入不带 ' 的十六进制数字#',它返回 UIColor 代码。例如橙色 (#f77f00) 的代码是:
The easiest way that I found: Hex to UIColor Converter
Just type the hex number without '#', and it returns the UIColor code. For example the code for the orange color (#f77f00) is:
我想我会将这六个字符分成三对,然后将其转换为十进制,然后除以 255 以获得每个颜色分量作为浮点数。
然后您可以将组件传递给:
I think I'd split the six characters into three pairs, then convert that to decimal, then divide that by 255 to get each color component as a float.
You can then pass the components to:
如果从 ObjectiveC 转换到 swift 对您来说很困难,那么 Swift 就是答案。目前它只接受不带 # 的字符串,但我相信您可以添加扫描仪方法来跳过它。
If converting from ObjectiveC to swift is hard for you, here's the answer with Swift. Currently it only takes strings without the #, but you can add a scanner method to skip it I believe.
我创建了一个在线工具,可以在不方便使用自定义方法或插件时立即将任何十六进制代码转换为 Swift 和 Objective-C 的 UIColor 代码片段:https://iosref.com/uihex/
I created an online tool to instantly convert any hex code to a UIColor code snippet for Swift and Objective-C, for when it's inconvenient to use custom methods or plugins: https://iosref.com/uihex/
如果您不想编写上面的所有代码,您可以查看此站点: http://www.diovo.com/apps/rgb-to-uicolor-converter.html
从像这样的十六进制颜色:#FFFFFF,该网站将其转换为如下字符串:
If you don't want to write all of this code above, you can check this site: http://www.diovo.com/apps/rgb-to-uicolor-converter.html
From an HEX color like this: #FFFFFF, the site convert it in a string like this:
不要忘记您可以选择将十六进制值转换为 RGB 并输入它们在界面生成器中。它将节省一些代码行。
Don't forget that you have the option to convert your hex values to RGB and enter them in the interface builder. It'll save some lines of code.
我猜这有点晚了……但我在 WhiteHouse Github 存储库中找到了这个版本,它以一种非常优雅的方式实现了这一点:
GitHub 上 WHAppConfig.m 的源链接
Guess this is a bit late here... but I found this version in the WhiteHouse Github repo that does it in a pretty elegant way:
Source Link to their WHAppConfig.m on github
我认为使用十六进制值有一种更简单的方法。
只需在文件顶部添加定义或引用头文件进行转换 (UIColorFromRGB)。您甚至可以添加固定十六进制颜色值的模板。
然后直接使用十六进制值或您定义的十六进制值在代码中引用它。
例如...
(PS - 假设 Alpha 为 1.0,但它始终可以在定义中更改)。
享受。
I think there is an even easier way when using HEX values.
Just add a definition at the top of your file or reference a header file for the conversion (UIColorFromRGB). You can even add a template of fixed HEX color values.
Then just reference it in your code using the HEX values directly or your defined hex values.
For example...
(PS - This assumes an Alpha of 1.0, but it can always be changed in the definition).
Enjoy.
我发现 cocoapod 库在使用“#RRGGBB”值创建 UIColor 方面非常有用。
I found a cocoapod library very useful in creating UIColor using "#RRGGBB" values.
{
}
{
}