Reputation: 21
I want to draw text onto my subclass on UIView
so that the text is cut out of the shape and the background behind the view shows through, just like in the OSX Mavericks logo found here.
IBOutlet UIView *ViewTest
IBOutlet UIlabel *labelTest alpha:0 style
(Show background image)http://s25.postimg.org/tkf1sa3x9/1122.png
I would say that I'm more of an intermediate/early advanced iOS developer so feel free to throw some crazy solutions at me. I'd expect I'd have to override drawRect
in order to do this.
Upvotes: 2
Views: 238
Reputation: 21573
One way would be to use layer mask. Render text onto an image, and set that image as the mask of a layer and assign that layer as the mask of your view.
// Create your mask layer
CALayer* maskLayer = [CALayer layer];
maskLayer.frame = CGRectMake(0,0,yourMaskWidth ,yourMaskHeight);
maskLayer.contents = (__bridge id)[myTextRenderedImage CGImage];
// Apply the mask to your uiview layer
yourUIView.layer.mask = maskLayer;
myTextRenderedImage
is the image you render plain text on.
Upvotes: 1