Fantattitude's LIFE
Home    Info    Website
About: Vivien Leroy's blog, things about code, life, games, mining <3

Easily create soft shadows for your UILabels

Hi everybody bonjour !

So I was wondering how I could do to make an UILabel cute in my UITableView headers.
I then decided that a soft shadow would be great to achieve this.

But oops. You can only make not blurred shadows with the shadow* properties from UILabel.

The trick here is to use Quartz 2D and the layer property of your UILabel (UIView subclasses gets it).

label.layer.shadowOpacity = 1.0; // Pretty self explanatory
label.layer.shadowRadius = 2.0; // Your softness :p
label.layer.shadowColor = [UIColor blackColor].CGColor; // Color of the shadow
label.layer.shadowOffset = CGSizeMake(0.0, 2.0); // Offset of the shadow

You could also use a UILabel subclass and CGContextSetShadowWithColor() but it’s way more complicated. (Well not really but we don’t care :P)

Also please note that this code is only compatible with iOS > 3.2.

See ya !