Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse layer mask for label

Tags:

ios

uilabel

How do you reverse the mask layer for a label? I have a textLabel, which I use as a mask for an imageView that contains an arbitrary image as follows:

let image = UIImage(named: "someImage")
let imageView = UIImageView(image: image!)

let textLabel = UILabel()
textLabel.frame = imageView.bounds
textLabel.text = "Some text"

imageView.layer.mask = textLabel.layer
imageView.layer.masksToBounds = true

The above makes the text in textLabel have a font colour of the imageView as in How to mask the layer of a view by the content of another view?.

How do I reverse this so as to remove the text in textLabel from the imageView?

like image 448
alexjrlewis Avatar asked Apr 21 '16 03:04

alexjrlewis


People also ask

How do I invert a layer mask in Photoshop?

Command + I (Mac ) | Control + I (Win) will invert a layer mask (or, click the Invert button on the Properties panel).

How do you make a mask negative in Photoshop?

Duplicate the layer. Make the bottom layer black and white. Give the top layer a negative layer mask (All black: Create by alt+clicking on the create layer mask button) Use the white brush to mask IN the color layer.

How does masking effect with text write its steps?

Small Text MaskingStep 1: Start a new composition. Step 2: Select the Solid layer with an attractive color. Step 3: With the help of the text tool, type your text. Step 4: Select a good style that suits your thoughts.

How does a masking layer work?

The mask first identifies a margin, or the area between the feature and the edge of the mask, and then uses an outline method to create the mask's polygon. To learn more about masking layer tools and their purposes, see An overview of the Masking toolset.

How to invert a layer mask in Photoshop?

Now any adjustments you make will affect the mask and not your image! The easiest way to invert a layer mask in Photoshop is with the keyboard shortcut Command + I (Mac) or Control + I (PC). This will switch everything to the opposite color in your layer mask. Now black becomes white and white becomes black, swapping which areas are transparent.

Is it possible to mask labels on a layer?

The idea proposed is to go to the layer being labeled and designate specific layers that will mask the labels, just like how layer masking works under the Appearance ribbon. This idea should be re-opened. 12-06-2021 05:07 PM

What is Dynamic label masking?

Dynamic Label Masking is a very basic cartographic tool that can really speed up workflows. 11-19-2021 04:09 PM To do this you need to set the weight of the masking layer.


1 Answers

Make a subclass of UILabel:

class InvertedMaskLabel: UILabel {
    override func drawTextInRect(rect: CGRect) {
        guard let gc = UIGraphicsGetCurrentContext() else { return }
        CGContextSaveGState(gc)
        UIColor.whiteColor().setFill()
        UIRectFill(rect)
        CGContextSetBlendMode(gc, .Clear)
        super.drawTextInRect(rect)
        CGContextRestoreGState(gc)
    }
}

This subclass fills its bounds with an opaque color (white in this example, but only the alpha channel matters). Then it draws the text using the Clear blend mode, which simply sets all channels of the context back to 0, including the alpha channel.

Playground demo:

let root = UIView(frame: CGRectMake(0, 0, 400, 400))
root.backgroundColor = .blueColor()
XCPlaygroundPage.currentPage.liveView = root

let image = UIImage(named: "Kaz-256.jpg")
let imageView = UIImageView(image: image)
root.addSubview(imageView)

let label = InvertedMaskLabel()
label.text = "Label"
label.frame = imageView.bounds
label.font = .systemFontOfSize(40)
imageView.maskView = label

Result:

demo of image transparency inside the label text

like image 125
rob mayoff Avatar answered Sep 29 '22 09:09

rob mayoff