Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mask the layer of a view by the content of another view?

I have a UIImageView and a UILabel, and want the content of the UILabel to mask the UIImageView. The goal is that the text is visible with content of image but everything else transparent.

Is there a simple way to mask a view by the contents of another view?

like image 603
openfrog Avatar asked Jan 14 '23 16:01

openfrog


1 Answers

You can use QuartzCore Framework.

(Link project with QuartzCore.framework and import <QuartzCore/QuartzCore.h>).

@import QuartzCore;

Background of label must be clear color. Example:

self.imageView.layer.mask = self.label.layer;
self.imageView.layer.masksToBounds = YES;

In Interface Builder:

enter image description here

On device:

enter image description here

like image 51
Vitaly Berg Avatar answered Jan 16 '23 04:01

Vitaly Berg