2

What's the easiest way to draw an inner shadow in a UILabel, without using images?

I know you can use shadowColor and shadowOffset for drop shadows, but I didn't see any such properties for inner shadows.

  • It might help if you provide an example image of what you want. I am not sure if you are looking to make the text look recessed or some other visual effect. – Jon Steinmetz Aug 08 '11 at 21:20
  • Sure, here is an example of how inner shadow looks (from Google): http://sublimeorange.com/css/css3-inner-shadow/ –  Aug 08 '11 at 21:30

3 Answers3

4

Check out FXLabel. I've been using it recently and it works great.

https://github.com/nicklockwood/FXLabel

Colin Humber
  • 1,395
  • 1
  • 16
  • 23
1

I do not believe there is any easy way to accomplish this appearance. You will either need to use an image or a custom view that draws the shadow.

I would use your favorite image editor to make an image of the inner shadow composited against transparency (or a color if you need it) and load it into a stretchable UIImage. For example here is an inner shadow image (17px x 17px): inner shadow

You can load it like this:

UIImage* innerShadow = [ [ UIImage imageNamed: "innershadow.png" ] 
        stretchableImageWithLeftCapWidth: 8 topCapHeight: 8 ];
UIImageView* innerShadowView = [ [ [ UIImageView alloc ] initWithImage: innerShadow ] 
        autorelease ];
myLabel.backgroundColor = [ UIColor clearColor ];

Then place innerShadowView behind the label. You could generate the image programmatically as well if you wanted to avoid an image resource.

** Update based on comments follows **

Ah, based on your latest comment the effect you are looking for is completely different than the picture in the earlier comment.

In this case you will have to make a UILabel subclass and do your own drawing to composite the effect you are looking for. Take a look at this answer for how to composite the inner shadow effect.

Community
  • 1
  • 1
Jon Steinmetz
  • 4,104
  • 1
  • 23
  • 21
  • Hm, I guess this might work for `UIView`s, but I need a solution that works for both `UIView`s and `UILabel`s. –  Aug 08 '11 at 22:54
  • A UILabel is a subclass of UIView. Is there a specific reason you think this will not work for you? – Jon Steinmetz Aug 08 '11 at 23:08
  • Yes, but applying this to a UILabel would just put an inner shadow in the frame of the label. I need an inner shadow inside the actual text. Sorry if the first example was misleading, but it should be like this: http://download.oracle.com/javafx/2.0/visual_effects/img/inner.jpg –  Aug 08 '11 at 23:31
0

What you mean by any such properties for inner shadows. shadowOffset is the option to set the shadows (vertical or horizontal shadow) of the UILabel.

I do not know but this answer may be long to implement.

Community
  • 1
  • 1
Praveen-K
  • 3,401
  • 1
  • 23
  • 31