0

I am trying to create a multiline label in cocos2d 1.0 using CCLabelTTF. I have tried the examples that I have come across, but none seem to work. Here is what I have

CCLabelTTF *storeLabelHeading = [CCLabelTTF labelWithString:@"Here is a really long string that I want to wrap"
                                                     dimensions: CGSizeMake(200,0)
                                                      alignment: NSTextAlignmentCenter
                                                  lineBreakMode: NSLineBreakByWordWrapping
                                                       fontName:@"Marker Felt" fontSize: 24];

storeLabelHeading.color = ccc3(0,0,0);
[storeLabelHeading setAnchorPoint:ccp(0,0)];

storeLabelHeading.position = ccp(screenSize.width * 0.35f,
                                       screenSize.height * 0.85);
[self addChild:storeLabelHeading z:kStoreLayer+10];

I have tried a variety of dimensions. If I use CGSizeMake(0,0), then the label will display, but no wrap (which I think is expected). But any other value results in nothing being displayed. What am I doing wrong?

JeffB6688
  • 3,782
  • 5
  • 38
  • 58
  • (200,0) .... try making the bounding box of an appropriate size. I overshoot, and set the vertical alignment to center. – YvesLeBorg Oct 01 '13 at 15:42
  • @YvesLeBorg I tried that also. Setting the vertical dimension to 0 is supposed to make it have no bounds. But I have tried non-zero values and that does not work either – JeffB6688 Oct 01 '13 at 15:52
  • Cocos2D v2.x has a similar issue with iOS 9 devices - even when compiled using iOS SDK 9. iOS 8 devices still line breaks. Update: Line break on iOS9 devices didn't work with `kCCVerticalTextAlignmentCenter` for some reason, when I changed to `kCCVerticalTextAlignmentTop` it magically started to work... – Jonny Oct 14 '15 at 07:16

2 Answers2

3

As per your question, i get the same results with cocos2d 2.0, no word wrap. However, I got this to work properly :

    CCTexture2D *tex =[ [[CCTexture2D alloc] 
            initWithString:@"Here is a really long string that I want to wrap wrap wrap"
                dimensions:CGSizeMake(120, 120)
                hAlignment:kCCTextAlignmentCenter  
                vAlignment:kCCVerticalTextAlignmentCenter
             lineBreakMode:kCCLineBreakModeWordWrap
                  fontName:@"Marker Felt"
                  fontSize:24 ] autorelease];

    CCSprite *spr = [CCSprite spriteWithTexture:tex];
    [self addChild:spr];
    spr.position=ccp(kScreenWidth/2,kScreenHeight/2);

strangely enough, while going through the CCLabelTTF ctor, it fails. Yet, CCLabelTTF uses this to create the label. It is probably related to the vertical alignment being mis-handled somewhere in the pipeline.

ps : this also works

    CCLabelTTF *storeLabelHeading = [CCLabelTTF labelWithString:@"Here is a really long string that I want to wrap"
                                                         dimensions: CGSizeMake(120,120)
                                                          hAlignment: kCCTextAlignmentLeft
                                                      lineBreakMode: kCCLineBreakModeWordWrap
                                                           fontName:@"Marker Felt" fontSize: 24];
    storeLabelHeading.verticalAlignment=kCCVerticalTextAlignmentCenter;

    storeLabelHeading.color = ccc3(0,0,0);
    [storeLabelHeading setAnchorPoint:ccp(0,0)];

    storeLabelHeading.position = ccp(kScreenWidth * 0.35f,
                                           kScreenHeight * 0.85);
    [self addChild:storeLabelHeading z:1+10];

    [storeLabelHeading setString:@"Here is a really long string that I want to wrap wrap wrap"];

Setting the string after setting the vertical alignment to center 'unbreaks' the CCLabelTTF ctor.

YvesLeBorg
  • 9,070
  • 8
  • 35
  • 48
  • Since I am using cocos2d v1.0, I couldn't get the second suggestion to work because verticalAlignment does not appear to be implemented. But I did get the first suggestion to work. – JeffB6688 Oct 01 '13 at 19:34
0

You need to give the height dimension of the label too. Right now you are passing in 200, 0 , try passing in a non-zero height

Mark
  • 876
  • 1
  • 7
  • 11