0

I am trying to add a CAShapeLayer to UILabel.
The issue is: the text disappears after doing so:

let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.fillColor = UIColor.yellow.cgColor
item.layer.addSublayer(shapeLayer)

Is there any other way to do this?

jscs
  • 63,095
  • 13
  • 148
  • 192
karthikeyan
  • 523
  • 2
  • 7
  • 24
  • https://stackoverflow.com/questions/4850149/adding-a-cggradient-as-sublayer-to-uilabel-hides-the-text-of-label or https://stackoverflow.com/questions/34962668/hide-text-with-a-shape-mask-swift ? – Larme Jul 12 '17 at 09:27
  • i tried both it was not helping me – karthikeyan Jul 12 '17 at 09:33
  • 1
    the easy way is to use two UIViews, reason from [this answer](https://stackoverflow.com/a/7209543/4222801) – JoShin Jul 12 '17 at 10:50

1 Answers1

6

I solved this issue, by adding Lable into UIView, And We have to insert layer like below

let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPat
shapeLayer.fillColor = UIColor.yellow.cgColor
item.layer.insertSublayer(shapeLayer, at: 0)

item object will be your UIView

I was tried to addLayer so it was not working as excepted.

item.layer.addSublayer(shapeLayer)
karthikeyan
  • 3,708
  • 1
  • 14
  • 25