1
votes
CCLabelTTF *label = [CCLabelTTF labelWithString:@"Vertically Center Me About Myself" dimensions:CGSizeMake(100, 300) alignment:UITextAlignmentCenter lineBreakMode:UILineBreakModeWordWrap fontName:@"Helvetica" fontSize:20];
label.position = ccp(screenwidth/2, screenheight/2);
[layer addChild:label];

So I create the above label and add it to my layer. Due to the dimensions parameter, the text does wrap correctly, but only the very first line of text is centered vertically (eg only the very first line of text has exactly 50% of the character height above the half screen mark, and exactly 50% of the character height beneath the half screen mark:

.
.
---Vertically---
   Center Me
  About Myself

Where the ----- denotes 1/2 screen (ignore the dots, they are just there for stackoverflow formatting). How do I get it to dynamically center the text about whatever the center of itself? So for example in this case, I would want 'Center Me' to be what is centered. If my string only contained 'Vertically' I would expect that to be centered, etc....

1

1 Answers

1
votes

Try to write after the first string:

label.anchorPoint = ccp([label boundingBox].size.width / 2, [label boundingBox].size.height / 2);

The second possible solution is to set its position directly upper according to the label boundingBox size