ali alali
ali alali

Reputation: 21

iOS UIView subclass draw see-through text to background

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.

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

Answers (1)

0xSina
0xSina

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

Related Questions