Question

It seem not possible to use text kit, attributed string and layout manager with Sprit Kit SKLabelNode.

I wonder how Sprite Kit game display Long text which need to break into multiple line and Attributed String?

Was it helpful?

Solution

Attributed strings and line wrapping are not supported by SKLabelNode.

I'm afraid if you need that it's up to you to implement it. You can use multiple label nodes to display multiple lines, and possibly write a custom tool that slices attributed, newline strings into multiple labels line-spaced and colored accordingly. Perhaps a library like that already exists.

The alternative is to display a regular text view on top of the SKView.

OTHER TIPS

You might want to check out ASAttributedLabelNode, a SKLabelNode implementation with support for attributed strings.

https://github.com/alex-alex/ASAttributedLabelNode

As of iOS 11 SKLabelNode DOES seem to support attributed strings and automatic line breaks. New properties have been added to the documentation of the class:

Be warned: SpriteKit updates have a history of introducing buggy new features so you'll probably have to test these for yourself.


Note about performance of SKLabelNode:

There's some advice floating around that you should convert your labels to textures and use SKSpriteNodes to render them for better performance. I don't know how much of a difference this actually makes but it might make sense for smaller labels with text you never change.

SKView has a member function that converts a node into a texture:

- (SKTexture *)textureFromNode:(SKNode *)node;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top