Apply Inner Shadow to UILabel

I want to apply an inner shadow to a UILabel. I have a solution, but that’s not enough. Anyone with a better solution?

// UILabel subclass

- (void) drawTextInRect:(CGRect)rect {
    CGSize myShadowOffset = CGSizeMake(0, 2);
    float myColorValues[] = {255, 0, 0, 1};

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

    CGColorSpaceRef myColorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef myColor = CGColorCreate(myColorSpace, myColorValues);

    CGContextSetShadowWithColor (myContext, myShadowOffset, 5, myColor);
    CGContextSetBlendMode(myContext, kCGBlendModeLighten);

    [super drawTextInRect:rect];

    CGColorRelease(myColor);
    CGColorSpaceRelease(myColorSpace); 

    CGContextRestoreGState(myContext);
}

I am familiar with the property of the UILabel layer, but shadow offsetit gives us an outer shadow, not an inner shadow (if I don't see something).

+5
source share
3 answers

I tried to do this, but finally decided to use the standard one shadowOffsetand play with shadowColorto give the text an inner shadow. In small texts, it gives a good effect of inner shadow. For example, if you have a grayColor background and apply whiteColor to the shadow, then you have an acceptable inner shadow effect.

.

+4

, , .. , ( ), , , . (: ):

cell.textLabel.textColor     = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5];
cell.textLabel.shadowColor   = [UIColor darkGrayColor];
cell.textLabel.shadowOffset  = CGSizeMake(-1.0,-1.0);
[cell.textLabel setText:@"Welcome to MyApp!"];

inset UILabel text

, , , , .

, , shadowOffset, .

+21

Answer here: Inner shadow in UILabel Long code, but seems to work

+3
source

All Articles