11

I have been using the following code in my game. The Problem is that i am unable to make multi-line label in spritekit as i was able to do using CCLabelTTF...... Can Somebody help me. Also i am unable to use either \t or \n in my code... Thanks for the reply in advance

SKLabelNode *winner = [SKLabelNode labelNodeWithFontNamed:@"Arial"];
winner.text = @"Another\nTest";
winner.fontSize = 32;
winner.fontColor = [SKColor blueColor];
winner.position = CGPointMake(100 , 160);
[self addChild:winner];
STT LCU
  • 4,348
  • 4
  • 29
  • 47
Umesh Sharma
  • 388
  • 5
  • 23

4 Answers4

7

I had the same problem. I created a drop-in replacement for SKLabelNode called DSMultilineLabelNode that supports word wrap, line breaks, etc. The underlying implementation draws the string into a graphics context and then applies that to a texture on an SKSpriteNode.

It's available on GitHub at:

https://github.com/downrightsimple/DSMultilineLabelNode

Chris Allwein
  • 2,498
  • 1
  • 20
  • 24
5

It appears that SKLabelNode only supports one line of text at a time. I can't find it in the SpriteKit documentation or anywhere else official, but it's mentioned on the 47th slide of this WWDC presentation. You'll have to use UILabel instead, which you can see explained here.

Community
  • 1
  • 1
doctorBroctor
  • 2,018
  • 1
  • 14
  • 17
  • 1
    I know this is an old answer, but the second part of this answer bothers me, "Use UILabel instead." Last I knew `UILabel` can not be used inside of an `SKScene`, and explained here only explains how to use a `UILabel`, not how to use it in an `SKScene`. The only way I can think of is you can capture the rendered contents from a hidden `UILabel`, and place it into a `SKSpriteNode` (I am actually trying to avoid this approach, which brought me to this question) – Knight0fDragon Feb 02 '16 at 19:17
5

You can use NSLayoutManager to line-break the string to match a desired width, as hinted by this question.

Note that NSLayoutManager is available on iOS beginning with iOS 7.

Community
  • 1
  • 1
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
3

Like a lot of others I also needed a fix to this problem. My approach was a lot simpler than Chris' solution. I have made a subclass of SKLabelNode called NORLabelNode.

It's available at GitHub and there is also a cocoapod available.

It simply creates a set of SKLabelNodes and use these as subnodes. If you want to implement something similar yourself the main gist is this method:

- (NSArray *)labelNodesFromText:(NSString *)text{
    NSArray *substrings    = [text componentsSeparatedByString:@"\n"];
    NSMutableArray *labelNodes    = [[NSMutableArray alloc] initWithCapacity:[substrings count]];

    NSUInteger labelNumber    = 0;
    for (NSString *substring in substrings) {
        SKLabelNode *labelNode    = [SKLabelNode labelNodeWithFontNamed:self.fontName];
        labelNode.text    = substring;
        labelNode.fontColor    = self.fontColor;
        labelNode.fontSize    = self.fontSize;
        labelNode.horizontalAlignmentMode    = self.horizontalAlignmentMode;
        labelNode.verticalAlignmentMode    = self.verticalAlignmentMode;
        CGFloat y    = self.position.y - (labelNumber * self.fontSize * self.lineSpacing);
        labelNode.position    = CGPointMake(self.position.x, y);
        labelNumber++;
        [labelNodes addObject:labelNode];
    }

    return [labelNodes copy];
}

The above is somewhat simplified as the labels also inherit most of the other properties from their parent.

The linespacing can be altered through a CGFloat property. Apart from this is works just like an ordinary SKLabelNode and you can change text, color, font, fontSize etc. on the fly whenever you feel like.

T. Benjamin Larsen
  • 6,373
  • 4
  • 22
  • 32