Skip to content

Instantly share code, notes, and snippets.

@tmdvs
Created December 5, 2011 15:14
Show Gist options
  • Save tmdvs/1433902 to your computer and use it in GitHub Desktop.
Save tmdvs/1433902 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];
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];
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];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment