我有一个简单的CCLayer子类,其中有一个圆形作为背景图像。我也有一个CCLabelTTF作为子节点。如何使标签的文本在背景图像上居中?
我现在拥有的是:
// in MyCCLayer's init method
CCSprite *backgroundImage = [CCSprite spriteWithFile:@"circle.png"];
[self addChild:backgroundImage];
self.contentSize = backgroundImage.contentSize;
CCLabelTTF *label = [CCLabelTTF labelWithString:@"0"
dimensions:self.contentSize
alignment:UITextAlignmentCenter
fontName:@"Arial"
fontSize:32];
[self addChild:label];我试着改变anchorPoint和标签上的位置,但是我不能让文本仅仅集中在背景图像上。文本总是偏移的。我想要居中文字,而不管字体大小。
发布于 2012-08-30 01:01:44
我假设查看您的代码,您的标签已经结束在底部的圆圈?如果您创建的标签如下所示,它应该适用于您。
CCLabelTTF *label = [CCLabelTTF labelWithString:@"0" fontName:@"Arial" fontSize:32];
label.anchorPoint = ccp(0.5, 0.5);
label.position = ccp(backgroundImage.contentSize.width/2, backgroundImage.contentSize.height/2);发布于 2014-01-06 14:05:33
try this code:
CCSprite *backgroundImage = [CCSprite spriteWithFile:@"circle.png"];
[self addChild:backgroundImage];
backgroundImage.position=ccp(winSize.width/2,winSize.height/2);
CCLabelTTF *label = [CCLabelTTF labelWithString:@"0"
dimensions:self.contentSize
alignment:UITextAlignmentCenter
fontName:@"Arial"
fontSize:32];
label.position=backgroundImage.position;
[self addChild:label];https://stackoverflow.com/questions/12163343
复制相似问题