[Tutorial] Custom UIColors

Discussion in 'iOS Development' started by cocotutch, Jul 30, 2009.

  1. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Open up Xcode and choose a View Based Application.

    Please Register or Log in to view images



    Go to File > New File and choose an Objective-C Class. Name this file set "ColorC" and make sure "Make ColorC.h" is ticked too.

    Please Register or Log in to view images



    Open up ColorC.h and edit it to look like this:

    Please Register or Log in to view images



    And then open up ColorC.m and edit it to look like this:

    Please Register or Log in to view images



    Now go to your <ProjectName>ViewController.m file.

    Usage:
    [OBJC][self.view setBackgroundColor:[UIColor colorFromRGBIntegers:255 green:165 blue:214 alpha:1.0]];[/OBJC] This colour is Pale Pink.

    [OBJC][self.view setBackgroundColor:[UIColor colorFromRGBIntegers:204 green:119 blue:34 alpha:1.0]];[/OBJC] This colour is Ochre.

    [OBJC][self.view setBackgroundColor:[UIColor colorFromRGBIntegers:175 green:238 blue:238 alpha:1.0]];[/OBJC] This colour is Pale Blue.

    A very easy way to make colours for this, is open Photoshop's Colour Picker, find a colour you simply adore and copy in the RGB values into Xcode for that same colour to appear on your device/simulator.

    Don't forget to #import "ColorC.h" at the top of your file! But if you #import "ColorC.h" in your <ProjectName>_prefix.pch file it will auto import it for every class file in the same project

    Please Register or Log in to view images



    Enjoy your custom UIColors!

    cocotutch

    P.S. If you are compiling for Device, add:

    [OBJC]#define UIColorFromRGB(rgbValue) [UIColor \
    colorWithRed

    Please Register or Log in to view images

    (float)((rgbValue & 0xFF0000) >> 16))/255.0 \
    green

    Please Register or Log in to view images

    (float)((rgbValue & 0xFF00) >> 8))/255.0 \
    blue

    Please Register or Log in to view images

    (float)(rgbValue & 0xFF))/255.0 alpha:1.0][/OBJC]

    To your <ProjectName>_Prefix.pch file in Xcode.
  2. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    OR you can use this one line of code...

    [OBJC][UIColor colorWithRed:231 green:142 blue:234 alpha:0.5];
    [/OBJC]
  3. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    True. But I have a CGColor based Ball object in my project and I had to discover another way for it to work without crashing

    Please Register or Log in to view images

  4. spoonforknife

    spoonforknife Retired Moderator

    Joined:
    Nov 15, 2007
    Messages:
    6,896
    Likes Received:
    317
    Unless I'm painfully wrong, any value above 1 will yield white. You must divide those values by 255, like he did in the ColorC header.
  5. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    I just put random numbers, in there because I couldn’t be bothered searching/creating a actual colour. I was referring to the method I used more.
  6. spoonforknife

    spoonforknife Retired Moderator

    Joined:
    Nov 15, 2007
    Messages:
    6,896
    Likes Received:
    317
    Of course. Yeah, the custom method is nice to have though, for something where you're using a lot of different colo(u)rs being copied from Photoshop or something.
  7. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    Sorry I don't understand what you mean "copied from photoshop" in the end, we both put in the same types of values. I mean you could get a colour from photoshop and use it in the method I provided as well.....
  8. spoonforknife

    spoonforknife Retired Moderator

    Joined:
    Nov 15, 2007
    Messages:
    6,896
    Likes Received:
    317
    I mean the raw RGB values out of anything else. Yes, you could divide the RGB value by 255 each time you enter a value, but having a method do it for you is easier.
  9. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Do you have any idea what an integer is? Because your method accepts floats, not integers. They are NOT the same thing, not even close.

    Furthermore, your helper function "name" is VERY wrong. Please redo this the correct way.




    If you need a CGColor, use UIColor's CGColor property. Ie, [UIColor redColor].CGColor. Also, look into CoreGraphics if you ate going to use CoreGraphics. const float *components = CGColorGetComponents(CGColorRef aColor).
  10. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    I am not understanding you Skylar. It works perfectly for me:

    [OBJC]ball.color = [UIColor colorFromRGBIntegers:204 green:119 blue:34 alpha:1.0].CGColor;[/OBJC]

    That is how I use it. And ColorC is what I named the Header and Method files because I couldn't think of a name. This works with .CGColor, and I adapted this for general use in UIViews.

    And if it didn't accept Integers, why does it work? Floats may be different, but it would have given me a big fat warning if I had made a shocking mistake.

    Next time I try to contribute here, I would appreciate it if you didn't come across so condescending. A nice "Oh mate, Integers and Floats are very different, and the name of your method isn't self-explanatory. You need to make it easier to recognize and understand" would suffice.

    cocotutch

Share This Page