Как получить "строковое" значение атрибута CATextLayer в swift 4? - PullRequest
0 голосов
/ 14 мая 2018

Я пытаюсь получить доступ к текстовому значению в CATextlayer, я могу получить доступ к другим значениям, но этот строковый атрибут не может быть доступен, и он даже не отображается на intellisence.Есть ли другой способ получить к нему доступ?Я новичок в Swift. У меня есть что-то вроде ниже:

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"

, и я хочу получить доступ к этому значению атрибута строки, как

 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 Ответ

0 голосов
/ 15 мая 2018

Свойство sublayers объявлено как базовый класс [CALayer]

Чтобы получить значение из определенного CALayer подкласса , вам необходимо привести тип

if let textLayer = cameraView.layer.sublayers?[2] as? CATextLayer {
    print(textLayer.string!)
}
...