1

I'm trying to create a UILabel mask with an animatable UIView behind it. I'm trying to use this answer How to mask the layer of a view by the content of another view? but I am not having any luck..

I've created a UIImageView, and a transparent UILabel in my nib. In in my UIView subclass I do this.

override func didMoveToSuperview() {

    self.imageview.layer.mask = self.label.layer
    self.imageview.layer.masksToBounds = true

I've also made sure that the two are sitting right on top of eachother. Is there anything else I should know in order to make this work? When I run the simulator both the imageview, and the uilabel are missing. What am I doing wrong?

my uilabel config

enter image description here

here i try doing this by using a programmatically created UILabel not added to a superview

    let blah = UILabel(frame: self.asteroid.frame)
    blah.text = "yo yo yo"
    blah.font = UIFont(name: "Estrogen", size: 20)

    self.asteroid.layer.mask = blah.layer
    self.asteroid.layer.masksToBounds = true
Community
  • 1
  • 1
hamobi
  • 7,940
  • 4
  • 35
  • 64
  • Have you tried changing the text colour? – Ian MacDonald Jan 14 '15 at 19:18
  • yes, according to the answer i linked the text should be transparent.. but ive tried adding a color. i have the same issue. nothing shows up – hamobi Jan 14 '15 at 19:19
  • It seems to me that the *background* of the `UILabel` would be transparent, but the text of the `UILabel` would not be in order for this to work. – Ian MacDonald Jan 14 '15 at 19:24
  • i dont have any background.. i can set the text itself to any color but when i have that code in place, both my imageview, and my label disappear =/ – hamobi Jan 14 '15 at 19:26
  • `UILabel` defaults its background colour to white; you need to set it to clear. – Ian MacDonald Jan 14 '15 at 19:29
  • ive set it to clear in interface builder, i added a picture of my uilabel config – hamobi Jan 14 '15 at 19:30
  • And you've made sure that you didn't add `self.label` to a view? `mask` layers with `superlayer`s don't usually work well. – Ian MacDonald Jan 14 '15 at 19:33
  • the label exists within interface builder, so i suppose it is a child of a uiview automatically. I haven't added it to any other view. In the answer I linked, the person was setting up the elements via IB.. so i figured that was okay? – hamobi Jan 14 '15 at 19:36
  • i've tried what you suggested – hamobi Jan 14 '15 at 19:40
  • Try using the `bounds` instead of the `frame`. You'll want to be in the coordinate space of the thing you're trying to mask, making `(0,0)` the origin of the masked layer. – Ian MacDonald Jan 14 '15 at 19:46
  • i tried using bounds too. no good. have you tried doing this Ian? – hamobi Jan 14 '15 at 19:47
  • 1
    Not with a `UILabel`, no. I have done a lot of work with text rendering on iOS, though, so I'm actually not that surprised to learn that it can't be combined as easily as one might expect. Have a look at this answer: http://stackoverflow.com/a/22672945/2708650 . It's unfortunate that the answer that you posted at the top of your question doesn't work well for you; that would be the easiest way to do it. – Ian MacDonald Jan 14 '15 at 19:56
  • 6
    Look this question: http://stackoverflow.com/questions/22671245/objective-c-display-uiview-within-uilabel-text-mask – weso Jan 14 '15 at 20:11
  • i copy pasted the code using obj c and got it to work.. only problem is it doesnt seem animatable (the background image) :( gonna do more research – hamobi Jan 14 '15 at 20:39
  • this is really the best way to go about it.. using two images instead of a UILabel http://stackoverflow.com/questions/438046/iphone-slide-to-unlock-animation – hamobi Jan 14 '15 at 20:55
  • You can now use the `maskView` property on the `UIView` (added in iOS 8.0) to do this. [See this answer.](http://stackoverflow.com/a/35678866/2976878) – Hamish Feb 28 '16 at 11:58

0 Answers0