Ios 使用UILabel文本掩码显示UIView

Ios 使用UILabel文本掩码显示UIView,ios,cocoa-touch,uiview,uilabel,calayer,Ios,Cocoa Touch,Uiview,Uilabel,Calayer,我正在尝试将ui视图添加到ui标签,这样文本就是视图的遮罩,使我能够执行动画文本背景之类的操作(很像锁定屏幕上解锁标签的幻灯片) 我计划这样做的方式是使用视图的层上的mask属性将其屏蔽为文本的形状。但是,我无法找到将UILabel的文本形状作为CALayer的方法 这可能吗?我只能在UILabel中找到覆盖-(void)drawRect:方法的解决方案,但这不会给我太多的灵活性。如果您为iOS 8+构建,则会更灵活。但是,如果您正在寻找iOS 8之前的答案,请看这里。 谢谢你指点我。关键是使

我正在尝试将
ui视图
添加到
ui标签
,这样文本就是视图的遮罩,使我能够执行动画文本背景之类的操作(很像锁定屏幕上解锁标签的幻灯片)

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

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

如果您为iOS 8+构建,则会更灵活。但是,如果您正在寻找iOS 8之前的答案,请看这里。


谢谢你指点我。关键是使用
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)
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];
Swift:

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)
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];
在iOS 8.0中。现在,只需创建一个
ui标签
,用作
ui视图的掩码

目标-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)
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];
Swift 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()
color取自
overlayView

我创建了自定义maskLabel

核对答案

这就是结果。

有帮助吗?是的,非常感谢!我已经发布了我的实现作为答案。哦,这是对UIKit的一个很好的补充!我以前没见过。由于这更灵活,我将其标记为已接受的答案:)此外,由于问题最初标记为obj-c,我已将代码的obj-c版本添加到您的答案中。@JohnnyRockex是的,对不起!我是在yonks之前做的,忘了设置
CATextLayer
contentsScale
,所以在2倍或3倍的显示器上显示出来会非常糟糕。我已经纠正了这一点。