简体   繁体   中英

Inner shadow in a UILabel

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.

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

https://github.com/nicklockwood/FXLabel

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): 内心的阴影

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM