2014-03-26 42 views
6

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

我打算这样做的方式是使用视图的layer上的mask属性将其掩盖为文本的形状。但是,我找不到一种方法将UILabel的文本形状设置为CALayer

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

+2

请问[this](http://stackoverflow.com/questions/17817378/ios-uiview-subclass-draw-see-through-text-to-background)有帮助吗? – Linuxios

+0

是的,非常感谢!我已经发布了我的实现作为答案。 – Hamish

回答

6

在IOS 8.0 UIView added the maskView property。现在,只需创建一个UILabel作为面膜使用的UIView

的Objective-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) 

这就形成了一个清晰的UILabelUIColor.blueColor()颜色取自overlayView

+0

哦,这是UIKit的一个很好的补充!以前没见过。因为这更灵活,我会将它标记为已接受的答案:) – Hamish

+2

此外,由于问题最初是标记为obj-c,因此我已将一个obj-c版本的代码添加到您的答案中。 – Hamish

5

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) 
+0

它有效,但出来很像素,不是吗? –

+1

@JohnnyRockex对不起!我以前做过这个,忘了设置CATextLayer的'contentsScale',所以它在2x或者3x显示器上显得非常糟糕。我纠正了这一点。 – Hamish