我有一个模仿按钮的类。它包含一个标签,我试图将其居中对齐(水平)。无论我尝试什么,标签都停留在左侧,让我认为这比看起来更复杂。这是唯一的初始化/设置代码:
-(CCButtonLayer*)initWithText:(NSString*)text bgColor:(ccColor4B)color width:(GLfloat)width height:(GLfloat)height {
self = [super initWithColor:color width:width height:height];
self.lbl = [CCLabelTTF labelWithString:text fontName:@"Marker Felt" fontSize:22];
self.lbl.horizontalAlignment = kCCTextAlignmentCenter;
[self.lbl setHorizontalAlignment:kCCTextAlignmentCenter];
self.lbl.color = ccc3(0,0,0);
// self.lbl.contentSize = CGSizeMake(width, height); // no effect anyway
self.lbl.anchorPoint = CGPointZero;
self.lbl.position = CGPointZero;
[self addChild:self.lbl];
return self;
}