UILabel的影子

时间:2015-06-19 10:03:54

标签: ios objective-c uilabel

我尝试使用以下代码对UILabel进行子类化..但无法看到与Label with DropShadow类似的内容

- (void) drawRect:(CGRect)rect {
    CGSize myShadowOffset = CGSizeMake(4, -4);
    CGFloat myColorValues[] = {0, 0, 0, .8};

    CGContextRef myContext = UIGraphicsGetCurrentContext();
    CGContextSaveGState(myContext);

    CGColorSpaceRef myColorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef myColor = CGColorCreate(myColorSpace, myColorValues);
    CGContextSetShadowWithColor (myContext, myShadowOffset, 5, myColor);

    self.layer.shadowOffset = CGSizeMake(0, 1);
    self.layer.shadowOpacity = 0.5;
    self.shadowColor = [UIColor blackColor];

    [super drawTextInRect:rect];

    CGColorRelease(myColor);
    CGColorSpaceRelease(myColorSpace);

    CGContextRestoreGState(myContext);
}

2 个答案:

答案 0 :(得分:2)

self.layer.shadowOffset = CGSizeMake(0, -2.5);
self.layer.shadowRadius = 1;
self.layer.shadowOpacity = 0.5;
self.layer.shadowColor = [UIColor blackColor].CGColor;

Here is the output

答案 1 :(得分:1)

UILabel类默认具有阴影属性。

levelLbl.shadowColor = [[UIColor blackColor] colorWithAlphaComponent:1]; levelLbl.shadowOffset = CGSizeMake(2, 2);

P.S。对于像上图一样的阴影,您必须将shadowRadius设置为layer

P.S.2如果您没有使用#import <QuartzCore/QuartzCore.h>或更晚,请不要忘记Xcode 6