लेबल के लिए रिवर्स परत मुखौटा आप लेबल के लिए मास्क परत को कैसे उलटते हैं? मैं एक textLabel
, जो मैं एक imageView
कि एक मनमाना छवि इस प्रकार होता है के लिए एक मुखौटा के रूप में उपयोग किया है:लेबल
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
ऊपर बना देता है textLabel
में पाठ How to mask the layer of a view by the content of another view? में के रूप में imageView
की एक फ़ॉन्ट रंग की है।
मैं यह कैसे रिवर्स तो के रूप में textLabel
imageView
से में पाठ निकालते हैं?
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)
}
}
यह उपवर्ग एक अपारदर्शी रंग (इस उदाहरण में सफेद है, लेकिन केवल अल्फ़ा चैनल मामलों) के साथ अपनी सीमा को भर देता है: