如何在swift 4中获取CATextLayer的“string”属性值?

时间:2018-05-14 12:07:39

标签: swift catextlayer

我正在尝试访问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 :(得分:0)

sublayers属性被声明为基类[CALayer]

要从特定的CALayer 子类获取值,您必须转换类型

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