xcode 如何将 NSString 转换为 UIColor
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8228339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert NSString to UIColor
提问by Xander
I have a pickerView which has a component of colors.
我有一个pickerView,它有一个颜色组件。
I have a textField (textOther), now I have NSMutableArray of all the available colors which are explicitly defined.
我有一个 textField (textOther),现在我有明确定义的所有可用颜色的 NSMutableArray。
How do I pass all those colors(which are strings!) to these textField?
我如何将所有这些颜色(它们是字符串!)传递给这些 textField?
textField setTextColor will set only a single color, but to pass string to a color how do I do it?
textField setTextColor 将只设置一种颜色,但是将字符串传递给一种颜色我该怎么做?
Thanks!
谢谢!
采纳答案by Warkst
I suppose the cleanest way to do this would be by making an NSDictionary
with predefined UIColor
instances, and map them as key value pairs; keys being the string that represents the color, value being the actual UIColor
object.
我想最简洁的方法是NSDictionary
使用预定义的UIColor
实例创建一个,并将它们映射为键值对;键是代表颜色的字符串,值是实际的UIColor
对象。
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 an NSDictionary
like this:
因此,如果您希望同时使用红色和蓝色作为可能的颜色(NSMutableArray
如果我理解正确的话,现在是一种颜色),您可以NSDictionary
这样做:
NSArray * colorKeys = [NSArray arrayWithObjects:@"red", @"blue" nil];
NSArray * colorValues = [NSArray arrayWithObjects:[UIColor redColor], [UIColor blueColor], nil];
NSDictionary * colorMap = [NSDictionary dictionaryWithObjects:colorValues forKeys:colorKeys];
If you then want to pass a color (given a string AKA the key of said color), you can simply do this:
如果你想传递一种颜色(给定一个字符串 AKA 所述颜色的键),你可以简单地这样做:
NSString * chosenColor = textField.text; // extract desired color from input in textField
textField.textColor = [colorMap valueForKey:chosenColor]; // use this color string to find the UIColor element in the colorMap
Hope this answers your question!
希望这能回答你的问题!
回答by Aitul
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。你应该有一个 NSString ,例如 whiteColor 或 blueColor ,你可以通过以下方式转换它:
-(UIColor *)getColor:(NSString*)col
{
SEL selColor = NSSelectorFromString(col);
UIColor *color = nil;
if ( [UIColor respondsToSelector:selColor] == YES) {
color = [UIColor performSelector:selColor];
}
return color;
}
回答by Fatih Aksu
To convert from NSString to UIColor:
要将 NSString 转换为 UIColor:
NSString *redColorString = @"25";
NSString *greenColorString = @"82";
NSString *blueColorString = @"125";
UIColor *color = [UIColor colorWithRed:[redColorString floatValue]/255.0
green:[greenColorString floatValue]/255.0
blue:[blueColorString floatValue]/255.0
alpha:1.0];