【问题标题】:Erasing text from a drawn UIView从绘制的 UIView 中擦除文本
【发布时间】:2018-10-18 03:33:59
【问题描述】:

我正在尝试绘制一个 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 或 UIImage。

【问题讨论】:

标签: ios objective-c uiview core-graphics


【解决方案1】:

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

您可以像这样使用标签和视图来做到这一点:

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)

【讨论】:

  • 你是对的。我不知道该怎么称呼它,因为通常掩码会将 alpha 视为要显示的像素,而我想要相反。你的答案虽然有效。谢谢!
猜你喜欢
  • 1970-01-01
  • 2013-03-15
  • 1970-01-01
  • 2021-07-15
  • 2012-06-08
  • 1970-01-01
  • 2013-02-15
  • 1970-01-01
  • 2011-11-03
相关资源
最近更新 更多