Search code examples
swiftalignmentscenekitarkit

How may I solve 'Cannot assign value of type 'CATextLayerAlignmentMode' to type 'String'?


func createNewBubbleParentNode(_ text: String) -> SCNNode {
    let billBoardConstraint = SCNBillboardConstraint()
    billBoardConstraint.freeAxes = SCNBillboardAxis.Y
    
    let bubble = SCNText(string: text, extrusionDepth: CGFloat(bubbleDepth))
    var font = UIFont(name: "Futura", size: 0.15)
    bubble.font = font
    bubble.alignmentMode = kCAAlignmentCenter
    
    return bubbleNodeParent
}

How can I solve 'Cannot assign value of type 'CATextLayerAlignmentMode' to type 'String' with this type of function?

Thanks for your answers!!

Loïc


Solution

  • All you need to do is to use a value CATextLayerAlignmentMode.center.rawValue. This value is used not only in iOS but also in macOS.

    bubble.alignmentMode = CATextLayerAlignmentMode.center.rawValue
    

    Here's a full code version:

    import ARKit
    import SceneKit
    
    class ViewController: UIViewController {
    
        @IBOutlet var sceneView: ARSCNView!
        
        override func viewDidLoad() {
            super.viewDidLoad()
            sceneView.scene = SCNScene()
            
            let textNode = self.createBubbleNode("SceneKit")
            sceneView.scene.rootNode.addChildNode(textNode)
    
            sceneView.session.run(ARWorldTrackingConfiguration())
        }
        
        func createBubbleNode(_ text: String) -> SCNNode {            
            let bubble = SCNText(string: text, extrusionDepth: 0.02)
            bubble.font = UIFont(name: "Futura", size: 0.15)
            bubble.firstMaterial?.diffuse.contents = UIColor.red
            bubble.alignmentMode = CATextLayerAlignmentMode.center.rawValue
            let bubbleNode = SCNNode(geometry: bubble)
            return bubbleNode
        }
    }
    

    enter image description here