如何将 NSString 转换为 UIColor

发布于 2024-12-17 13:50:54 字数 203 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅笑轻吟梦一曲 2024-12-24 13:50:54

最好创建一个将 NSString 转换为 UIColor 的方法。你应该有一个 NSString 例如白色或蓝色,你可以通过以下方式转换它:

  -(UIColor *)getColor:(NSString*)col

  {

  SEL selColor = NSSelectorFromString(col);
  UIColor *color = nil;
  if ( [UIColor respondsToSelector:selColor] == YES) {

    color = [UIColor performSelector:selColor];
   }  
    return color;
  }

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:

  -(UIColor *)getColor:(NSString*)col

  {

  SEL selColor = NSSelectorFromString(col);
  UIColor *color = nil;
  if ( [UIColor respondsToSelector:selColor] == YES) {

    color = [UIColor performSelector:selColor];
   }  
    return color;
  }
岁月静好 2024-12-24 13:50:54

要从 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];

To convert from NSString to 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];
红焚 2024-12-24 13:50:54

我认为最简洁的方法是使用预定义的 UIColor 实例创建 NSDictionary 并将它们映射为键值对;键是表示颜色的字符串,值是实际的 UIColor 对象。

因此,如果您希望同时使用红色和蓝色作为可能的颜色(如果我理解正确的话,它们现在位于 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];

如果您想传递一种颜色(给定一个字符串,又称为所述颜色的键),您可以简单地执行以下操作:

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

希望这能回答您的问题!

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.

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:

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:

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文