Scntext Alignment Not Working in iOS

SCNText Alignment not working in iOS

It looks like alignment is working -- try adding a second line to your test string like "TEST\nT" and switching between left and right alignment modes. But SCNText adapts to fit the coordinate system of its node, so alignment alone won't necessarily make the text fit into your scene the way you want it to.

You can tweak how a node's content fits into its parent space using the node's pivot property. Set it to a translation transform (SCNMatrix4MakeTranslation) based on the bounding box of your text and you can change the layout however you like.

Swift Scenekit - Centering SCNText - the getBoundingBoxMin:Max issue

OK so my final code solution looks like:

func setCounterValue(counterValue:Int) {

var v1 = SCNVector3(x: 0,y: 0,z: 0)
var v2 = SCNVector3(x: 0,y: 0,z: 0)

_textNode.removeFromParentNode()
_counterValue = counterValue

let newText = SCNText(string: String(format: "%08d", counterValue), extrusionDepth:sDepth)
newText.font = UIFont (name: "Arial", size: 3)
newText.firstMaterial!.diffuse.contents = UIColor.whiteColor()
newText.firstMaterial!.specular.contents = UIColor.whiteColor()

_textNode = SCNNode(geometry: newText)
_textNode.getBoundingBoxMin(&v1, max: &v2)

let dx:Float = Float(v1.x - v2.x)/2.0
let dy:Float = Float(v1.y - v2.y)
_textNode.position = SCNVector3Make(dx, dy, Float(sDepth/2))

node.addChildNode(_textNode)

}

I've left in my couple of global variables, but should make sense.

Thanks for the help all.

SceneKit - SCNText centering incorrectly

The difference of SCNText from other geometries is that SCNText origin point positioned at bottom left corner. In other geometries, it is a bottom center.

To fix text position in parent node you can set its pivotPoint.x to half of width:

SCNVector3 min, max;
[textNode getBoundingBoxMin:&min max:&max];
textNode.pivot = SCNMatrix4MakeTranslation((max.x - min.x) / 2, 0, 0);

To fix subnodes position, you should set their position to half of width plus min:

SCNVector3 min, max;
[textNode getBoundingBoxMin:&min max:&max];
subnode.position = SCNVector3Make((max.x - min.x) / 2 + min.x, (max.y - min.y) / 2 + min.y, 0);

Center multi-line SCNtext on macOS SceneKit

It seems that it's the use of NSAttributedString that breaks the alignment. Removing the following line works as a workaround.

txt.string = NSAttributedString(string: "This\nis\ncentered", attributes: [.foregroundColor : NSColor.black])

Note that in any case SceneKit will use the SCNMaterial of the geometry to shade the object and won't take NSAttributedString.Key.foregroundColor into account.

NSAttributedString doesn't work with SCNText

In direct answer to your question: "Why is the 3D text not colored?"

Colour is probably not considered part of the "style" as they're using the word in the docs. It's probably also the wrong word to be using, since it's a description of qualities of text that gets bent and twisted in every place it's ever used.

It's likely what they mean to say is things like weight, alignment, underlining, strikethroughs, kerning and leading... things of that nature are interpreted by SceneKit. But only trial and error will find for sure what does and does not work.

Apple should be providing a table with a list of attributes/qualities of text recognised and used by SceneKit.

The only use of "style" I can find in the Core Text and NSAttributedString docs suggest those frameworks consider the word to group these sorts of qualities per paragraph, which is also little to no help when thinking about SceneKit text.


Edit: Additional stuff about 3D and Materials, just in case this is news to you:

'Materials' in 3D speak create the impressions of colour, response to light, reflectiveness and other qualities of a material on an object in 3D, and need to be created and applied. It's not nearly as simple as saying "make objectABC be red" as it is in 2D thinking.

A single text object (in 3D speak, not how you think of these things in Cocoa) probably wouldn't have a way to automagically be made with different materials and/or Material IDs within SceneKit. Material IDs are how different materials are applied to different portions of a single object in things like 3ds Max where most people create geometry and materials.

Unfortunately SceneKit doesn't follow this convention, instead using 'elements' as a part of geometry objects, each of which is part of an index that corresponds to whatever materials your providing that geometry.

You can read more about this here:

https://developer.apple.com/library/ios/documentation/SceneKit/Reference/SCNGeometry_Class/index.html#//apple_ref/doc/uid/TP40012000-CLSCHSCNGeometry-SW15

So to get what you're looking for you're probably going to need to make 3 text objects and give each a unique material of the colour you're wanting, or break the 3 word object up into 3 elements and then have fun guessing how to get the right material onto each of them.

EDIT: Ignore last part of above paragraph. You need to make 3 text objects to get the look you're after. The elements feature of material distribution in Scene Kit is reserved on Text Objects, so you can't break up the text at the word level into differing Elements.

From docs:

A text geometry may contain one, three, or five geometry elements:

  • If its extrusionDepth property is 0.0, the text geometry has one
    element corresponding to its one visible side.

  • If its extrusion depth is greater than zero and its chamferRadius
    property is 0.0, the text geometry has three elements, corresponding
    to its front, back, and extruded sides.

  • If both extrusion depth and chamfer radius are greater than zero, the
    text geometry has five elements, corresponding to its front, back,
    extruded sides, front chamfer, and back chamfer.

SCNText Not Displaying Emoji's - ARKit in Swift

SCNText - developer.apple.com

"SceneKit can create text geometry using any font and style supported
by the Core Text framework, with the exception of bitmap fonts (such
as those that define color emoji characters)".

However, you can still put emoji's onto a SCNNode (say box) as material/texture.

Edit:
I created a Country flag ios App using emoji's a few months ago which is a good demo Youtube video - Country Flags iOS App



Related Topics



Leave a reply



Submit