【问题标题】:How may I solve 'Cannot assign value of type 'CATextLayerAlignmentMode' to type 'String'?如何解决“无法将“CATextLayerAlignmentMode”类型的值分配给“String”类型?
【发布时间】:2021-04-04 13:08:11
【问题描述】:
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
}

如何解决'无法将'CATextLayerAlignmentMode'类型的值分配给使用此类函数的'String'类型?

谢谢你的回答!!

洛伊克

【问题讨论】:

    标签: swift alignment scenekit arkit


    【解决方案1】:

    您需要做的就是使用值CATextLayerAlignmentMode.center.rawValue。此值不仅在 iOS 中使用,在 macOS 中也使用。

    bubble.alignmentMode = CATextLayerAlignmentMode.center.rawValue
    

    这是一个完整的代码版本:

    import ARKit
    
    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
        }
    }
    

    【讨论】:

      【解决方案2】:

      我认为您需要将 kCAAlignmentCenter 替换为 CATextLayerAlignmentMode.center.rawValue

      【讨论】:

      • 这太有用了!非常感谢!!
      猜你喜欢
      • 1970-01-01
      • 2015-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多