Skip to content

Instantly share code, notes, and snippets.

@tmdvs
Created December 5, 2011 15:16
Show Gist options
  • Save tmdvs/1433922 to your computer and use it in GitHub Desktop.
Save tmdvs/1433922 to your computer and use it in GitHub Desktop.
Subtracting a string from inside a CALayer. Like an cut out mask in Photoshop.
CALayer *myLayer = [CALayer layer];
[myLayer setFrame:rect];
[myLayer setBackgroundColor:[myColour CGColor]];
[myLayer setCornerRadius:radius];
UIGraphicsBeginImageContext([myLayer frame].size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
[myLayer renderInContext:context];
CGContextRestoreGState(context);
CGContextSetBlendMode(context, kCGBlendModeClear);
[myString drawInRect:myStringsBounds withFont:[UIFont boldSystemFontOfSize: 11]];
CGContextSetBlendMode(context, kCGBlendModeNormal);
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[outputImage drawInRect:rect];
@baldoph
Copy link

baldoph commented Dec 5, 2011

wow niice!
can you draw an image instead at line 17 and it subtracts it from the layer??

@tmdvs
Copy link
Author

tmdvs commented Dec 5, 2011

@baldoph I believe so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment