如何将 NSString 转换为 UIColor
我有一个 pickerView ,它有一个颜色组件。
我有一个文本字段(textOther),现在我有明确定义的所有可用颜色的 NSMutableArray。
我如何将所有这些颜色(它们是字符串!)传递给这些文本字段?
textField setTextColor 将仅设置单一颜色,但要将字符串传递给颜色我该怎么做?
谢谢!
I have a pickerView which has a component of colors.
I have a textField (textOther), now I have NSMutableArray of all the available colors which are explicitly defined.
How do I pass all those colors(which are strings!) to these textField?
textField setTextColor will set only a single color, but to pass string to a color how do I do it?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最好创建一个将 NSString 转换为 UIColor 的方法。你应该有一个 NSString 例如白色或蓝色,你可以通过以下方式转换它:
It´s better to create a method to convert for NSString to UIColor. You shoud have a NSString with for example whiteColor or blueColor and you can convert it in the following way:
要从 NSString 转换为 UIColor:
To convert from NSString to UIColor:
我认为最简洁的方法是使用预定义的 UIColor 实例创建 NSDictionary 并将它们映射为键值对;键是表示颜色的字符串,值是实际的 UIColor 对象。
因此,如果您希望同时使用红色和蓝色作为可能的颜色(如果我理解正确的话,它们现在位于 NSMutableArray 中),您可以创建一个像这样的 NSDictionary :
如果您想传递一种颜色(给定一个字符串,又称为所述颜色的键),您可以简单地执行以下操作:
希望这能回答您的问题!
I suppose the cleanest way to do this would be by making an
NSDictionary
with predefinedUIColor
instances, and map them as key value pairs; keys being the string that represents the color, value being the actualUIColor
object.So if you'd want to have both red and blue as possible colors (which are now in an
NSMutableArray
if I understood correctly), you'd make anNSDictionary
like this:If you then want to pass a color (given a string AKA the key of said color), you can simply do this:
Hope this answers your question!