iPhone UILabel text soft shadow

Dimitris picture Dimitris · Nov 10, 2009 · Viewed 102.3k times · Source

I know soft shadows are not supported by the UILabel out of the box, on the iPhone. So what would be the best way to implement my own one?

EDIT:

Obviously I will subclass the UILabel and draw in the -drawRect: My question is, how do I get the contents of the label as graphics and draw around them, blur them etc...

EDIT 2:

I returned to this question about a year later. In the meantime I've built a class that allows you to easily add soft shadow to a label and tweak it's radius etc and also to draw gradients on the text itself. You can find it on GitHub: https://github.com/doukasd/iOS-Components/tree/master/Views

Answer

IlDan picture IlDan · Jan 20, 2011

As of 3.2 there is direct support for shadows in the SDK.

label.layer.shadowColor = [label.textColor CGColor];
label.layer.shadowOffset = CGSizeMake(0.0, 0.0);

#import <QuartzCore/QuartzCore.h> and play with some parameters:

label.layer.shadowRadius = 3.0;
label.layer.shadowOpacity = 0.5;

And, if you find your shadow clipped by the label bounds:

label.layer.masksToBounds = NO;

finally set

label.layer.shouldRasterize = YES;