我正在尝试绘制一个 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 的想法,但我不知道如何反转它以便将文本视为负空间。我能找到的关于反转蒙版的唯一问题是反转 CAShapeLayers 或 UIImages。
最佳答案
我注意到你用的是橡皮擦这个词。在设计界,这种效果被称为 mask 。蒙版将隐藏层的部分,显示下面的内容。
您可以像这样使用标签和 View 来做到这一点:
let view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
let label = UILabel(frame: view.frame)
label.text = "$48.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)
关于ios - 从绘制的 UIView 中删除文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50229261/