Etiket için maske katmanını nasıl tersine çeviriyorsunuz? Ben şöyle keyfi bir resim içeren bir imageView
için maske olarak kullanmak bir textLabel
vardır:Etiket için ters katman maskesi
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
yukarıda textLabel
metin How to mask the layer of a view by the content of another view? gibi imageView
bir yazı rengi var yapar.
Nasıl bu kadar olarakimageView
den textLabel
yılında metin kaldırmak bu tersine çeviririz?
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)
}
}
Bu alt sınıf, opak bir renk (beyaz, bu örnekte, sadece alfa kanalı konularda) ile sınır doldurur: