ios - 从绘制的 UIView 中删除文本

标签 ios objective-c uiview core-graphics

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

enter image description here

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

enter image description here

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/

相关文章:

ios - 向部分脱离 View 的模态视图添加关闭按钮

iphone - 支持指南针/地磁场的iOS设备列表

ios - 如何在 ios 的 Xamarin 谷歌地图组件中处理点击事件

ios - UISplitViewController从master返回到detail

ios - 出现弹出窗口时,在 iOS 8 中禁用 UISplitViewController 纵向到横向旋转

ios - 在动画移动对象(UIView)上未检测到点击手势

swift - 我可以使用 UIView(.xib 和类)作为多个 View 的 View 基础示例吗?

ios - ld : framework not found url_launcher on Flutter, xcode

objective-c - 访问存储在 iOS 设备上的文本文件

ios - 购买历史记录中的inApp购买待定