DDPWNAGE DDPWNAGE - 1 year ago 120
Objective-C Question

SKLabelNode multiple lines

I'm having a bit of trouble with displaying a label with multiple lines in an

. First off, I know that there is no
property or anything of the sort. I get there are GitHub projects out there that may be able to do it, but I would rather simply not use them.

The reason why I need multiple lines is due to the way my In-App Purchases work. Sometimes, descriptions can't all fit in one line. For example, rendered at the biggest DynamicType caption font size, the string:

Cheat and get 25% of the points needed for the "Chaos" theme!

would not fit on any of the iOS mobile phones on a portrait layout. I've tried calling this function:

-(NSString*)autoInsertLineBreaksInString:(NSString*)string {
NSLog(@"Function called with %@", string);
NSMutableArray* arrayOfSpaceIndexes = [NSMutableArray array];
NSMutableString* returnString = [string mutableCopy];
for (int i = 0; i < string.length; i++) {
if ([string characterAtIndex:i] == [@" " characterAtIndex:0]) {
NSLog(@"Adding %i to array of space indexes.", i);
[arrayOfSpaceIndexes addObject:@(i)];
} else {
NSLog(@"Not adding %i to array of space indexes.", i);
[returnString replaceCharactersInRange:NSMakeRange([[arrayOfSpaceIndexes objectAtIndex:(NSUInteger)(floorf((arrayOfSpaceIndexes.count - 1) / 2))] unsignedIntegerValue], 1) withString:@"\n"];
NSLog(@"Retruning %@", returnString);
return returnString;

This inserts
at the right spot and outputs this...

Cheat and get 25% of the
points needed for the "Chaos" theme.

...but the label node interprets
as a space, giving me the original string. I really don't want to use a second label node. Any ideas on how I can fix this?

Answer Source

Formalizing a comment into an answer: If you choose to go with multiple instances of SKLabelNode, you might create a wrapper SKNode and add all each SKLabelNode to the wrapper. When you add the wrapper to your scene, you can hide/show/move/etc all label nodes together just by managing the one wrapper.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download