Search code examples
iosswiftuilabelmaskuivisualeffectview

Add UILabel Mask to UIVisualEffectView


I have an UIImageView, UIVisualEffectView, and an UILabel on my ViewController. The UIVisualEffectView blurs the UIImageView behind it. I would like to mask the UIVisualEffectView with the text of the UILabel. Like this image:

enter image description here

How would I do this? I'm using Swift.


Solution

  • You need to assign your UILabel as maskView of your UIVisualEfectView and the trick is that you need add a container for your UIVisualEfectView and your UILabel and set the background color for this container view as UIColor.clear

    self.visualEfectView.mask = self.label
    self.visualEfectView.layer.masksToBounds = true
    

    View Hierarchy Setup

    enter image description here

    Result

    enter image description here