Search code examples
scenekitarkitscntext

SCNText, containerFrame, wrapping and ARKit


Has anyone managed to get an SCNText string wrapping correctly within a containerFrame in ARKit?

I've had a go, but the lines seem to be superimposed on top of each other, rather than being rendered vertically in sequence. If it was a problem with the size of the containerFrame being too small, I'd expect the string to just be truncated. It doesn't make a difference which truncation mode I use (...end / ...none / ..middle) etc.

https://www.dropbox.com/s/hfe01fm2bfvuvs0/IMG_1E372152586C-1.jpeg?dl=0

This is code from my SCNNode subclass, creating the extruded text in the init method. The same code works fine (with different sizes obviously) to produce wrapped, extruded text in a standard SceneKit view.

    let extrudedText = SCNText(string: definition.text, extrusionDepth: 0.1)
    extrudedText.font = UIFont(name: definition.fontname, size: 0.2)!

    extrudedText.containerFrame = CGRect(origin: .zero, size: CGSize(width: 1.8, height: 1.5))
    extrudedText.truncationMode = kCATruncationMiddle
    extrudedText.isWrapped = true
    extrudedText.alignmentMode = kCAAlignmentLeft


    let material = SCNMaterial.material(named: "rustediron-streaks")
    extrudedText.materials = [material]
    geometry = extrudedText

    // Update pivot of object to its center
    // https://stackoverflow.com/questions/44828764/arkit-placing-an-scntext-at-a-particular-point-in-front-of-the-camera
    let (min, max) = boundingBox
    let dx = min.x + 0.5 * (max.x - min.x)
    let dy = min.y + 0.5 * (max.y - min.y)
    let dz = min.z + 0.5 * (max.z - min.z)
    pivot = SCNMatrix4MakeTranslation(dx, dy, dz)

Solution

  • Answer from Apple: my font size was too small. If I use a "normal" font size and containing frame on the SCNText object, and then set a scale on the node that contains it, everything wraps as expected. Something like:

    extrudedText.font = UIFont(name: definition.fontname, size: 20)!  
    extrudedText.containerFrame = CGRect(origin: .zero, size: CGSize(width: 100.0, height: 500.0))
    ...
    scale = SCNVector3Make(0.01, 0.01, 0.01)