× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

[Code Snippet] iOS - Convert hex color to UIColor easy!

Rob Heller
Dec 23, 2015
<h2>WHY</h2> <p>Most people (myself included) use hex color values such as #99FF00 when coding, unfortunately there is no built in way to use hex values when making iPhone applications.</p> <p> </p> <h2>USAGE</h2> <p>Once you find a hex color value you like you can convert it like this:</p> <pre><code class="language-objectivec">UIColor *myColor = [self colorFromHexString:@"#99FF00"];</code></pre> <p> </p> <h2>COPY/PASTE METHOD</h2> <p>You don't need to understand or do anything with this just copy paste it then use it like the example above!</p> <pre><code class="language-objectivec">-(UIColor *)colorFromHexString:(NSString *)hexString { unsigned rgbValue = 0; NSScanner *scanner = [NSScanner scannerWithString:hexString]; [scanner setScanLocation:1]; // bypass '#' character [scanner scanHexInt:&amp;rgbValue]; return [UIColor colorWithRed:((rgbValue &amp; 0xFF0000) &gt;&gt; 16)/255.0 green:((rgbValue &amp; 0xFF00) &gt;&gt; 8)/255.0 blue:(rgbValue &amp; 0xFF)/255.0 alpha:1.0]; }</code></pre> <p> </p> <p> </p>
comments powered by Disqus