Display UIView with UILabel text mask

I am trying to add a UIView to a UILabel , so the text is a presentation mask, which allows me to do things like animated text backgrounds (like a slide to unlock a shortcut on a locked screen).

The way I planned to do this is to use the mask property in the layer view to hide it until the text forms. However, I cannot find a way to get the text form of UILabel as CALayer .

Is it possible? I can find solutions that override the -(void)drawRect: UILabel in UILabel , but that won't give me much flexibility.

+6
source share
3 answers

UIView added the UIView property in iOS 8.0. Now just create a UILabel to use as a mask for the 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]; 

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) 

This creates a clear UILabel color UIColor.blueColor() , taken from an overlayView .

+6
source

The mopsled solution is more flexible if you are building for iOS 8+. However, if you are looking for an answer for pre-iOS 8, here it is.


Thanks to Linuxios for pointing out this question . The key is to use CATextLayer instead of UILabel .

Objective-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 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]; 

Swift:

 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 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) 
+5
source

I am creating a custom Label mask.

Check out the answer fooobar.com/questions/966514 / ...

This is the result. enter image description here

0
source

All Articles