简体   繁体   English

CCLabelTTF多行标签不起作用

[英]CCLabelTTF multiline label not working

I am trying to create a multiline label in cocos2d 1.0 using CCLabelTTF. 我正在尝试使用CCLabelTTF在cocos2d 1.0中创建多行标签。 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). 如果我使用CGSizeMake(0,0),则将显示标签,但不显示换行(我认为这是预期的)。 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. 根据您的问题,我使用cocos2d 2.0获得了相同的结果,没有自动换行。 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. 奇怪的是,通过CCLabelTTF ctor时,它失败了。 Yet, CCLabelTTF uses this to create the label. 但是,CCLabelTTF使用它来创建标签。 It is probably related to the vertical alignment being mis-handled somewhere in the pipeline. 这可能与垂直对齐方式在管线中的某处处理不当有关。

ps : this also works 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"];

Setting the string after setting the vertical alignment to center 'unbreaks' the CCLabelTTF ctor. 在将垂直对齐方式设置为居中位置之后设置字符串,可“断开” 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 现在您要传递200,0,请尝试传递非零高度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM