CCLabelTTF多行标签不起作用

时间:2013-10-01 15:36:03

标签: cocos2d-iphone multiline cclabelttf

我正在尝试使用CCLabelTTF在cocos2d 1.0中创建多行标签。我已经尝试过我遇到过的例子,但似乎都没有。这就是我所拥有的

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];

我尝试了各种各样的尺寸。如果我使用CGSizeMake(0,0),那么标签将显示,但没有换行(我认为是预期的)。但任何其他值都不会显示任何内容。我做错了什么?

2 个答案:

答案 0 :(得分:3)

根据你的问题,我得到了与cocos2d 2.0相同的结果,没有自动换行。但是,我让它正常工作:

    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);
奇怪的是,在通过CCLabelTTF ctor时,它失败了。然而,CCLabelTTF使用它来创建标签。这可能与垂直对齐在管道中的某处错误处理有关。

ps:这也有效

    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"];

将垂直对齐设置为中心后设置字符串'unbreaks'CCLabelTTF ctor。

答案 1 :(得分:0)

您还需要给出标签的高度尺寸。现在你传递200,0,尝试传递非零高度