Colors

April 27th, 2009 by Chris Leave a reply »

Most colors you will set in Interface Builder rather than in Xcode on the beginning, but if you need to change it dynamically, I’m sorry you need to know it.

I will present you the way how to change the background color of UIImageView – in other words image, using UIColor. By image I mean simply placeholder for image, because UIImageView can be just a colored rectangle without any PNG/JPG at all. But UIColor is very flexible, using the same code, you can change not only background colors, but shadow shadowColor (for UILabel), textColor (also UILabel) and I’m sure there is even more properties I can’t remember in this particular moment.

The code is very simple (as usuall):

myImage.backgroundColor = [UIColor blueColor];

Above code will cause, that myImage’s frame will be blue.

You can experiment with a lot of colors like:

  • blackColor
  • brownColor
  • cyanColor
  • grayColor
  • greenColor

While in Xcode write “[UIColor " and press escape to a list of all possible ending and you will get a lot of standard colors you can use.

If you would like to make your image (or anything else) transparent in some percent you can use alpha property:

myImage.alpha = 0.5;

This will make your image half transparent.

RGB

Of course it's not all. iPhone / iPod touch are devices able to display millions of colors (so they said). I have never counted them but it's very easy to display any color giving the amount of red, green and blue... and alpha at once.

The code is also a single line:

	image.backgroundColor = [UIColor colorWithRed:121/255 green:42/255 blue:120/255 alpha:0.9];
Above sets the image's frame color RGB(121,42,120) which opacity is 90%. You need to know one thing about above structure especially the alpha part. If you set this color as a backgroundColor for UILabel, only the background will be transparent in 10%, the text will be visible in 100%.

Predefined textures

There are already to predefined textures you can use as a color. They are:
viewFlipsideBackgroundColor
flipsidetexture
groupTableViewBackgroundColor
groupedtexture

Depending of brightness of your screen viewFlipsideBackgroundColor looks like some kind of black material. I guess every iPhone user is familiar with the second one, it's the same background as grouped UITableView uses (see Settings application). You can use them like they were a normal color.

myImage.backgroundColor = [UIColor viewFlipsideBackgroundColor];
myImage.backgroundColor = [UIColor groupTableViewBackgroundColor];

One more thing

There is an interesting color clearColor. What does it to? Well maybe it wan't be important right now, as long as you can change any object's property alpha to zero. But clearColor will simply erase any color, very helpful in 2D graphics (Quartz).

Another one more thing

As usuall I prepared for you a project. An application where you can test any color you need changing the RGB and alpha using sliders and the preview of viewFlipsideBackgroundColor and groupTableViewBackgroundColor.

xcodeproj

Download the project

colorssimulator

Advertisement

Leave a Reply