简体   繁体   English

Cocos2d:如何将CCLabelTtf垂直对齐以不同的字体大小?

[英]Cocos2d: How to Align CCLabelTtf vertically with different font size?

How to aligh two CCLabelTtf vertically with different font size? 如何垂直对齐两个具有不同字体大小的CCLabelTtf? 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];

    }

屏幕截图

With cocos2d 2.x, use the following : 对于cocos2d 2.x,请使用以下命令:

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: 我注意到,使用各种2.1版本之一,您可能必须在设置垂直对齐方式之后执行此操作,以使其正常工作:

[label setString:inText];

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

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