从绘制的 UIView 中删除文本

Erasing text from a drawn UIView

我正在尝试绘制一个 UIView 并从中删除文本以产生与此类似的效果:

到目前为止,我还没有想出让文本充当橡皮擦的方法。这是我所在的位置:

CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(c, [UIColor whiteColor].CGColor);
CGContextFillRect(c, self.bounds);
CGContextSetBlendMode(c, kCGBlendModeClear);
CGContextSetTextDrawingMode(c, kCGTextFill);
CGContextSetFont(c, CGFontCreateWithFontName(CFStringCreateWithCString(NULL, "HelveticaNeue-Bold", kCFStringEncodingUTF8)));
CGContextSetFontSize(c, 18.0);
UIGraphicsPushContext(c);
[_text drawInRect:self.bounds
   withAttributes:nil];
UIGraphicsPopContext();

我希望 CGContextSetBlendMode(c, kCGBlendModeClear); 能让文本充当橡皮擦,但到目前为止,我还没有成功。

我也有过将 UILabel 用作 maskView 的想法,但我不知道如何反转它以便将文本视为负数 space。我能找到的关于反转蒙版的唯一问题是反转 CAShapeLayers 或 UIImages。

我注意到你用的是橡皮擦这个词。在设计界,这种效果被称为遮罩。蒙版将隐藏层的部分,显示下面的内容。

您可以像这样使用标签和视图来执行此操作:

let view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
let label = UILabel(frame: view.frame)
label.text = ".99"
label.font = UIFont.systemFont(ofSize: 70)
label.textAlignment = .center
label.textColor = UIColor.white

let underlyingView = UIView(frame: view.frame)
underlyingView.backgroundColor = UIColor.blue
underlyingView.mask = label

view.addSubview(underlyingView)