views:

3379

answers:

5

I know soft shadows are not supported by the UILabel our 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...

+2  A: 

Subclass UILabel, and override -drawInRect:

NSResponder
Okay that's the obvious part. Then what?
Dimitris
+5  A: 

This answer to this similar question provides code for drawing a blurred shadow behind a UILabel. The author uses CGContextSetShadow() to generate the shadow for the drawn text.

Brad Larson
That's great and it works as advertised! Thanks.But do you have an idea how I can make that shadow more intense?Since it part of the context, I can't draw it more than once, which would do the trick.
Dimitris
You could change the color to be black with a 100% opacity (in his example, it's only 80%), as well as change the shadow radius within the CGContextSetShadow() call. You might also try shifting the offset from the 4,-4 position that he's set.
Brad Larson
Yes I have tried 100% opacity etc. And regarding the size, the larger you set it to be, the less visible the shadow is... I am looking for something similar to the Shadow Effect in Flash, which you can set to more than 100%.
Dimitris
+1  A: 

Subclass UILabel, as stated, then, in drawRect:, do [self drawTextInRect:rect]; to get the text drawn into the current context. Once it is in there, you can start working with it by adding filters and whatnot. If you want to make a drop shadow with what you just drew into the context, you should be able to use:

CGContextSetShadowWithColor()

Look that function up in the docs to learn how to use it.

Sastira
A: 

I suppose you to use shadowColor, shadowOffset in UILabel.


UILabel* label = [[UILabel alloc] init];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0,1);
oksk
Thank you but yes. I was actually looking for *soft* shadow.
Dimitris
A: 

Apply the (soft) shadow on the view's layer, like this:

UILabel *label = [[UIabel alloc] init];
label.layer.shadowColor = [[UIColor whiteColor] CGColor];
label.layer.shadowOpacity = 1.0;
Yang