cocos2d-iphonealignmentcclabelttf

RIght-align and position a CCLabelTTF


I would expect the following code to position the label at the upper right of the screen. However the x position appears off to the right of the screen and I have to manually move it way over. What is wrong with this code?

CGSize winSize = [[CCDirector sharedDirector] winSize];

self.screenTop = winSize.height;
self.screenRight = winSize.width;
self.screenBottom = 0;
self.screenLeft = 0;


self.scoreLabel = [CCLabelTTF labelWithString:@"000000" dimensions:CGSizeMake(200,30) hAlignment:kCCTextAlignmentRight fontName:@"Helvetica" fontSize:24];
self.scoreLabel.position = ccp(self.screenRight - 20, self.screenTop - 20);
[self addChild:self.scoreLabel z:1];

The position is logged correctly as {548, 300} (iPhone5). It appears that hAlignment:kCCTextAlignmentRight doesn't work right.


Solution

  • self.scoreLabel.position = ccp(self.screenRight - 100, self.screenTop - 20);
    

    more generically :

    self.scoreLabel.position = ccp(self.screenRight - self.scoreLabel.contenSize.width/2,
    self.screenTop - self.scoreLabel.contenSize.height/2);