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,694
  • 13
  • 151
  • 195
karthikeyan
  • 3,821
  • 3
  • 22
  • 45
  • 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,821
  • 3
  • 22
  • 45