简体   繁体   中英

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?

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:

在此输入图像描述

On device:

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM