0

I am trying to access the text value in the CATextlayer, I can access other values but this string attribute cannot be accessed and it isn't even displayed on intellisence either. Is there some other way to access it ? I am new to swift.I have something like below:

let textLayer = CATextLayer()
textLayer.isHidden = true
textLayer.contentsScale = UIScreen.main.scale
textLayer.fontSize = 14
textLayer.font = UIFont(name: "textLayer = CATextLayer()
textLayer.isHidden = true
textLayer.contentsScale = UIScreen.main.scale
textLayer.fontSize = 14
textLayer.font = UIFont(name: "Avenir", size: textLayer.fontSize)
textLayer.alignmentMode = kCAAlignmentCenter", size:textLayer.fontSize)
textLayer.string = "someText"
textLayer.foregroundColor = textColor.cgColor
textLayer.backgroundColor = color.cgColor
textLayer.isHidden = false
textLayer.contentsScale = UIScreen.main.scale
textLayer.name="someText"

and I want to access this string attribute value like

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first

    guard let point = touch?.location(in: cameraView) else { return }

    print(self.selectedObject = cameraView.layer.sublayers![2].string!)

    self.selectedObject = cameraView.layer.sublayers![2].name!//Textlayer
  // print(cameraView.layer.sublayers![3])//shapelayer


}
  • 1
    Show more code. Show how `textLayer` is declared and initialized. – rmaddy May 14 '18 at 15:21
  • I have updated it. – Muhammad Taha May 15 '18 at 11:21
  • You declared it as `textLayer` not `textlayer` so `print(textlayer.string)` should be `print(textLayer.string)` as vadian already commented. – TheTiger May 15 '18 at 11:41
  • i did a work around using the name attribute of the CATextLayer since i was still unable to access it as it says "Value of type 'CALayer' has no member 'string' " . I have updated the code so that you have a clear idea how I am using it – Muhammad Taha May 15 '18 at 12:04

1 Answers1

1

The sublayers property is declared as the base class [CALayer]

To get a value from a particular CALayer subclass you have to cast the type

if let textLayer = cameraView.layer.sublayers?[2] as? CATextLayer {
    print(textLayer.string!)
}
vadian
  • 274,689
  • 30
  • 353
  • 361