使用UILabel文本掩码显示UIView

时间:2014-03-26 19:30:56

标签: ios cocoa-touch uiview uilabel calayer

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

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

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

3 个答案:

答案 0 :(得分:9)

iOS 8.0中的

UIView added the maskView property。现在,只需创建一个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];

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()颜色取自overlayView

答案 1 :(得分:5)

如果您为iOS 8+构建,

mopsled's solution会更灵活。但是,如果你正在寻找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)

答案 2 :(得分:0)

我创建了自定义maskLabel。

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

这是结果。 enter image description here