{"id":2137,"date":"2015-02-27T14:46:44","date_gmt":"2015-02-27T22:46:44","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=2137"},"modified":"2015-02-27T14:46:44","modified_gmt":"2015-02-27T22:46:44","slug":"setting-colors-with-an-nsstring","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=2137","title":{"rendered":"Setting colors with an NSString"},"content":{"rendered":"<p>I wanted to color some letters in an NSAttributed string using a random list of colors that I generated. What I wanted to do was something like this:<\/p>\n<p><pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\">\nNSString colorName = [NSString stringWithFormat:@&quot;%@Color&quot;, colorList[i] ];\nimageColor = [UIColor colorName];\n<\/code><\/pre><br \/>\nBut you can\u2019t feed an NSString to the UIColor method. Use a selector to do it.<br \/>\nLike this:<br \/>\n<pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\"> \nNSString *colorname = [NSString stringWithFormat:@&quot;%@Color&quot;, colorList[i] ];\nSEL labelColor = NSSelectorFromString(colorname);\nUIColor *imageColor = [Utilities uiColorFromColorName:colorname];\nimageColor = [UIColor performSelector:labelColor];\n<\/code><\/pre><\/p>\n<p>I had to create pink separately since it is not in the Apple supplied color list. However, I don\u2019t really like the other colors so I created a Utility method to create a UIColor from our RGB color values. It generates a UIColor that I can use for my attributed string.<\/p>\n<p><pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\">\nUIColor *imageColor = [Utilities uiColorFromColorName:[lettersExploded[2] lowercaseString] ];\n<\/code><\/pre><\/p>\n<p><pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\">\n+ (UIColor *)uiColorFromColorName:(NSString *)colorName {\n\nUIColor *imageColor = nil;\n\n&nbsp;&nbsp;&nbsp;&nbsp;if ( [colorName isEqualToString:@&quot;black&quot;] ) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imageColor = [UIColor colorWithRed:0.0f\/255.0f green:0.0f\/255.0f blue:0.0f\/255.0f alpha:1];\n&nbsp;&nbsp;&nbsp;&nbsp;} else if ( [colorName isEqualToString:@&quot;blue&quot;] ) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imageColor = [UIColor colorWithRed:8.0f\/255.0f green:41.0f\/255.0f blue:247.0f\/255.0f alpha:1];\n&nbsp;&nbsp;&nbsp;&nbsp;} else if ( [colorName isEqualToString:@&quot;brown&quot;] ) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imageColor = [UIColor colorWithRed:95.0f\/255.0f green:47.0f\/255.0f blue:0.0f\/255.0f alpha:1];\n&nbsp;&nbsp;&nbsp;&nbsp;} else if ( [colorName isEqualToString:@&quot;gray&quot;] ) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imageColor = [UIColor colorWithRed:140.0f\/255.0f green:140.0f\/255.0f blue:140.0f\/255.0f alpha:1];\n&nbsp;&nbsp;&nbsp;&nbsp;} else if ( [colorName isEqualToString:@&quot;green&quot;] ) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imageColor = [UIColor colorWithRed:51.0f\/255.0f green:153.0f\/255.0f blue:51.0f\/255.0f alpha:1];\n&nbsp;&nbsp;&nbsp;&nbsp;} else if ( [colorName isEqualToString:@&quot;orange&quot;] ) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imageColor = [UIColor colorWithRed:255.0f\/255.0f green:116.0f\/255.0f blue:0.0f\/255.0f alpha:1];\n&nbsp;&nbsp;&nbsp;&nbsp;} else if ( [colorName isEqualToString:@&quot;pink&quot;] ) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imageColor = [UIColor colorWithRed:255.0f\/255.0f green:90.0f\/255.0f blue:148.0f\/255.0f alpha:1];\n&nbsp;&nbsp;&nbsp;&nbsp;} else if ( [colorName isEqualToString:@&quot;purple&quot;] ) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imageColor = [UIColor colorWithRed:140.0f\/255.0f green:0.0f\/255.0f blue:140.0f\/255.0f alpha:1];\n&nbsp;&nbsp;&nbsp;&nbsp;} else if ( [colorName isEqualToString:@&quot;red&quot;] ) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imageColor = [UIColor colorWithRed:233.0f\/255.0f green:17.0f\/255.0f blue:0.0f\/255.0f alpha:1];\n&nbsp;&nbsp;&nbsp;&nbsp;} else if ( [colorName isEqualToString:@&quot;white&quot;] ) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imageColor = [UIColor colorWithRed:255.0f\/255.0f green:255.0f\/255.0f blue:255.0f\/255.0f alpha:1];\n&nbsp;&nbsp;&nbsp;&nbsp;} else if ( [colorName isEqualToString:@&quot;yellow&quot;] ) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imageColor = [UIColor colorWithRed:255.0f\/255.0f green:255.0f\/255.0f blue:0.0f\/255.0f alpha:1];\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;return imageColor;\n}\n\n<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wanted to color some letters in an NSAttributed string using a random list of colors that I generated. What I wanted to do was something like this: NSString colorName = [NSString stringWithFormat:@&quot;%@Color&quot;, colorList[i] ]; imageColor = [UIColor colorName]; But you can\u2019t feed an NSString to the UIColor method. Use a selector to do it. &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=2137\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Setting colors with an NSString<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-2137","post","type-post","status-publish","format-standard","hentry","category-ios"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2137"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2137\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}