Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
887 views
in Technique[技术] by (71.8m points)

ios - Cocos2d: How to Align CCLabelTtf vertically with different font size?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...