I'm trying to add a UIView
to a UILabel
, so that the text is the view's mask, enabling me to do things like animated text backgrounds (much like the slide to unlock label on the lockscreen).
The way I was planning on doing it was using the mask
property on the view's layer
to mask it to the shape of the text. However, I cannot find a way to get the UILabel
's text shape as a CALayer
.
Is this even possible? I can only find solutions that override the -(void)drawRect:
method in the UILabel
, but this wouldn't give me much flexibility.
UIView
added themaskView
property in iOS 8.0. Now, just create aUILabel
to use as a mask for aUIView
:Objective-C:
Swift 2:
This creates a crisp
UILabel
withUIColor.blueColor()
color taken fromoverlayView
.I create the custom maskLabel.
Check the answer https://stackoverflow.com/a/47105664/7371852
This is the result.
mopsled's solution is more flexible if you building for iOS 8+. However, if you're looking for a pre-iOS 8 answer, here it is.
Thanks to Linuxios for pointing me to this question. The key is to use
CATextLayer
instead ofUILabel
.Objective-C:
Swift: