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?

有帮助吗?

解决方案

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.

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top