Inner Shadow in UILabel

后端 未结 6 1722
庸人自扰
庸人自扰 2020-12-04 05:10

is it possible to create such a UILabel with inner and outer shadow?

alt text http://dl.getdropbox.com/u/80699/Bildschirmfoto%202010-07-12%20um%2021.28.57.png

<
6条回答
  •  感情败类
    2020-12-04 05:41

    Although Steve's answer does work, it didn't scale to my particular case and so I ended up creating an inner shadow by applying a shadow to a CGPath after clipping my context:

    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // clip context so shadow only shows on the inside
    CGPathRef roundedRect = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:4].CGPath;
    CGContextAddPath(context, roundedRect);
    CGContextClip(context);
    
    CGContextAddPath(context, roundedRect);
    CGContextSetShadowWithColor(UIGraphicsGetCurrentContext(), CGSizeMake(0, 0), 3, [UIColor colorWithWhite:0 alpha:1].CGColor);
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithWhite:0 alpha:1].CGColor);
    CGContextStrokePath(context);
    

    Which results in:

    enter image description here

    You can change the shadow offset and blur to change the shadow style. If you need it to be darker, you can also successively add and then stroke the same CGPath, which will make the shadows pile up.

提交回复
热议问题