0

How to aligh two CCLabelTtf vertically with different font size? Thanks

This is how I am creating labels:

for (int i = 0; i < 5; i++) {
        CCLabelTTF *label = [CCLabelTTF labelWithString:@"11km 90m" fontName:@"Limelight.ttf" fontSize:20];
        label.anchorPoint = ccp(0.5, 0.5);
        label.position = ccp((size.width-225)/2 + 30, size.height  - (size.height-260)/2 -i * 40 - 28);
         [self addChild:label];

        CCLabelTTF *label1 = [CCLabelTTF labelWithString:@"2013/11/22" fontName:@"Limelight.ttf" fontSize:15];
        label1.anchorPoint = ccp(0.5, 0.5);
        label1.position =  ccp(160 + 25, size.height  - (size.height-260)/2 -i * 40 - 28);
        [self addChild:label1];

        CCLabelTTF *label2 = [CCLabelTTF labelWithString:@"21 : 00" fontName:@"Limelight.ttf" fontSize:10];
        label2.anchorPoint = ccp(0.5, 0.5);
        label2.position =  ccp(size.width - ((size.width-225)/2 + 30) + 20, size.height  - (size.height-260)/2 -i * 40 - 28);
        [self addChild:label2];

    }

screen shot

Vervatovskis
  • 2,277
  • 5
  • 29
  • 46
  • and what seems to be the problem ? – YvesLeBorg Dec 06 '13 at 14:33
  • label1, label2 and label3 are not aligned because of the different size font, the biggest font size is the higher label is (a little bit higher) – Vervatovskis Dec 06 '13 at 14:35
  • @Yves I added a screen shot. Thanks – Vervatovskis Dec 06 '13 at 14:39
  • what version of cocos2d are you using ? it matters – YvesLeBorg Dec 06 '13 at 15:52
  • cocos2d version 1.0.1 – Vervatovskis Dec 06 '13 at 15:57
  • ah, hou may be cold out of luck. I dont recall the details of the API at that version other than it was an issue. Try to see if you can create a texture using CCTexture2D , and later create the label with texture. You may have more control over the vertical alignment. Otherwise, i would encourage you to forklift to cocos2d version 2.1, where all this works (see my ... precipitous answer below). – YvesLeBorg Dec 06 '13 at 16:02

1 Answers1

0

With cocos2d 2.x, use the following :

CCLabelTTF *label = [CCLabelTTF labelWithString:inText
                                       fontName:_defaultMenuItemFont
                                       fontSize:_defaultMenuItemFontSize
                                     dimensions:inSize
                                     hAlignment:inAlignment

];
label.verticalAlignment = kCCVerticalTextAlignmentCenter;

i noticed that with one of the various 2.1 builds, you may have to do this AFTER setting the vertical alignment in order for it to work properly:

[label setString:inText];
YvesLeBorg
  • 9,070
  • 8
  • 35
  • 48
  • 1
    well, work in 'debug' and all deprecated calls will be still functioning. Look in the 'ccDeprecated' class, for each deprecated API call it will show the appropriate replacement syntax. The one i find most annoying is the renaming of the .rect property to .activeArea ..... grrrrrrr. Did you look at CCTexture2D under 1.0.1 ? – YvesLeBorg Dec 06 '13 at 17:16
  • Frankly no, I just started (One month). I think I should migrate – Vervatovskis Dec 08 '13 at 16:25
  • yes, especially if you dont have a large code base to migrate. And if this is your first objC stint, you may want to give ARC a try. – YvesLeBorg Dec 08 '13 at 18:33