【问题标题】:Display UIView with a UILabel text mask使用 UILabel 文本掩码显示 UIView
【发布时间】:2014-05-05 11:02:37
【问题描述】:

我正在尝试将UIView 添加到UILabel,以便文本是视图的掩码,使我能够执行动画文本背景之类的操作(很像解锁锁定屏幕上的标签的幻灯片)。

我计划这样做的方式是使用视图的layer 上的mask 属性将其屏蔽为文本的形状。但是,我找不到将UILabel 的文本形状作为CALayer 的方法。

这甚至可能吗?我只能在UILabel 中找到覆盖-(void)drawRect: 方法的解决方案,但这不会给我太大的灵活性。

【问题讨论】:

  • this 有帮助吗?
  • 是的,非常感谢!我已经发布了我的实现作为答案。

标签: ios cocoa-touch uiview uilabel calayer


【解决方案1】:

UIView added the maskView property 在 iOS 8.0 中。现在,只需创建一个UILabel 用作UIView 的掩码:

目标-C:

UILabel *label = [[UILabel alloc] initWithFrame:self.view.frame];
label.text = @"Label Text";
label.font = [UIFont systemFontOfSize:70];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];

UIView* overlayView = [[UIView alloc] initWithFrame:self.view.frame];
overlayView.backgroundColor = [UIColor blueColor];
overlayView.maskView = label;
[self.view addSubview:overlayView];

斯威夫特 2

let label = UILabel.init(frame: view.frame)
label.text = "Label Text"
label.font = UIFont.systemFontOfSize(70)
label.textAlignment = .Center
label.textColor = UIColor.whiteColor()

let overlayView = UIView.init(frame: view.frame)
overlayView.backgroundColor = UIColor.blueColor()
overlayView.maskView = label
    
view.addSubview(overlayView)

这会创建一个清晰的UILabel,其中UIColor.blueColor() 颜色取自overlayView

【讨论】:

  • 哦,这是对 UIKit 的一个很好的补充!以前没见过。由于这更灵活,我会将其标记为已接受的答案:)
  • 另外,由于问题最初被标记为 obj-c,因此我在您的答案中添加了 obj-c 版本的代码。
【解决方案2】:

mopsled's solution 在为 iOS 8+ 构建时会更灵活。但是,如果您正在寻找 iOS 8 之前的答案,这里就是。


感谢Linuxios 将我指向this question。关键是使用CATextLayer 而不是UILabel

目标-C:

CGRect textRect = {0, 100, self.view.frame.size.width, 100}; // rect to display the view in

CATextLayer *textMask = [CATextLayer layer];
textMask.contentsScale = [UIScreen mainScreen].scale; // sets the layer's scale to the main screen scale
textMask.frame = (CGRect){CGPointZero, textRect.size};
textMask.foregroundColor = [UIColor whiteColor].CGColor; // an opaque color so that the mask covers the text
textMask.string = @"Text Mask"; // your text here
textMask.font = (__bridge CFTypeRef _Nullable)([UIFont systemFontOfSize:30]); // your font here
textMask.alignmentMode = kCAAlignmentCenter; // centered text

UIView* view = [[UIView alloc] initWithFrame:textRect];
view.backgroundColor = [UIColor blueColor];
view.layer.mask = textMask; // mask the view to the textMask
[self.view addSubview:view];

斯威夫特:

let textRect = CGRect(x: 0, y: 100, width: view.frame.size.width, height: 100) // rect to display the view in

let textMask = CATextLayer()
textMask.contentsScale = UIScreen.mainScreen().scale // sets the layer's scale to the main screen scale
textMask.frame = CGRect(origin: CGPointZero, size: textRect.size)
textMask.foregroundColor = UIColor.whiteColor().CGColor // an opaque color so that the mask covers the text
textMask.string = "Text Mask" // your text here
textMask.font = UIFont.systemFontOfSize(30) // your font here
textMask.alignmentMode = kCAAlignmentCenter // centered text

let bgView = UIView(frame: textRect)
bgView.backgroundColor = UIColor.blueColor()
bgView.layer.mask = textMask // mask the view to the textMask
view.addSubview(bgView)

【讨论】:

  • @JohnnyRockex 是的,对不起!我之前做过这个,忘记设置CATextLayercontentsScale,所以它在2x 或3x 显示器上会非常糟糕。我已经更正了。
【解决方案3】:

我创建了自定义 maskLabel。

检查答案 https://stackoverflow.com/a/47105664/7371852

这就是结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多