I messed around with this for like 30 minutes before I got it working, hopefully it might save another newbie some time.
It just lets you create a block of multi line text on the screen using labels without breaking up words in the middle.
- (void) setTipString:(NSString*)str {
NSInteger lineChars = 0;
BOOL isSpace = NO;
NSInteger index = 0;
NSInteger numLines = 0;
NSMutableString *line = [NSMutableString stringWithCapacity:LINE_LENGTH];
while (index <= [str length]) {
if(index == [str length]) {
BitmapFontAtlas *tip = [[BitmapFontAtlas bitmapFontAtlasWithString:[NSString stringWithString:line]
fntFile:@"text.fnt"
alignment:UITextAlignmentLeft]
retain];
[tip setPosition: cpv(30,210 - 20 * numLines)];
[self addChild:tip];
return;
}
NSString *tmp = [str substringWithRange:NSMakeRange(index, 1)];
[line appendString:tmp];
if([tmp isEqual:@" "])
isSpace = YES;
else
isSpace = NO;
if(lineChars >= LINE_LENGTH && isSpace) {
BitmapFontAtlas *tip = [[BitmapFontAtlas bitmapFontAtlasWithString:[NSString stringWithString:line]
fntFile:@"text.fnt"
alignment:UITextAlignmentLeft]
retain];
[tip setPosition: cpv(30,210 - 20 * numLines)];
[self addChild:tip];
lineChars = -1;
[line setString:@""];
numLines++;
}
lineChars++;
index++;
}
}
In case I got the tags wrong again, the code is up on my site here http://johnehartzog.com/2009/06/create-multi-line-labels-with-cocos-2d-iphone/.