Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>EDIT: Two years later, this post still get some karma and comments. Fixed my old answer to a better one.</p> <p>The most sensible, and reusable way to add a function which can take input between 0 and 255 for UIColor, is to create a custom category. Easier to read, easier to debug, easier for other people to contribute to, and keeps the project clean and structured as it grows beyond just a view viewcontrollers. So, add the following files, and import them in your m-files whereever you need them</p> <p>UIColor+Extra.h</p> <pre><code>@interface UIColor (Extra) + (UIColor *)colorWithR:(uint)red G:(uint)green B:(uint)blue A:(uint) alpha + (UIColor *) randomColor; + (UIColor *) colorWithHex:(uint) hex; @end </code></pre> <p>UIColor+Extra.m</p> <pre><code>#import "UIColor+Extra.h" @implementation UIColor (Extra) + (UIColor *)colorWithR:(uint)red G:(uint)green B:(uint)blue A:(uint) alpha { return [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:alpha/100.f]; } + (UIColor *) randomColor { CGFloat red = (CGFloat)random()/(CGFloat)RAND_MAX; CGFloat blue = (CGFloat)random()/(CGFloat)RAND_MAX; CGFloat green = (CGFloat)random()/(CGFloat)RAND_MAX; return [UIColor colorWithRed:red green:green blue:blue alpha:1.0]; } + (UIColor *) colorWithHex:(uint) hex { int red, green, blue, alpha; blue = hex &amp; 0x000000FF; green = ((hex &amp; 0x0000FF00) &gt;&gt; 8); red = ((hex &amp; 0x00FF0000) &gt;&gt; 16); alpha = ((hex &amp; 0xFF000000) &gt;&gt; 24); return [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:alpha/255.f]; } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload