2012-08-18 5 views
1

버튼을 모방 한 클래스가 있습니다. 여기에는 중앙에 (수평으로) 정렬하려고하는 하나의 레이블이 들어 있습니다. 내가 무엇을 시도하든, 레이블은 왼쪽면에 머물러있어 이것이 내가 생각하는 것보다 더 복잡하다고 생각하게 만든다. 여기에 유일한 초기화/설정 코드는 다음과 같습니다CCLabelTTF 텍스트 정렬

-(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; 
} 

답변

3

레이블의 정렬 특성을 제거하고 대신 수동으로 정렬했습니다. 분명히 내가 볼 수없는 무언가가 일어나고 있습니다. @ LearnCocos2D

// this worked 
CGSize textSize = [self.lbl.string sizeWithFont:[UIFont fontWithName:fontName size:fontSize]]; 
self.lbl.anchorPoint = CGPointZero; 
self.lbl.position = ccp(textSize.width /2.0f, textSize.height/2.0); 
1
self.lbl.anchorPoint = CGPointZero; 
self.lbl.position = CGPointZero; 

그것은 레이블의 위치를 ​​결정하는이 두 줄입니다.

self.lbl.anchorPoint = ccp(0.5, 0.5); //This is default, you could omit 
self.lbl.position = ccp(self.contentSize.width/2.0f, self.contentSize.height/2.0f); 

나는 "자기"버튼이 있다고 가정하고 제임스는 anchorPoint가 영향을 미치는 변경 말했듯이 컨텐츠 크기가이 시점

+0

불행히도 효과가 없습니다. – Echilon

+0

그 시점에서 'self.contentSize'는 무엇입니까? –

1

에 설정되어 있다는 :

로 설정해보십시오 조정. CGPointZero를 할당하지 마십시오. 왼쪽 하단 모서리를 부모 위치로 정렬하기 때문입니다.

여전히 변경 사항이 표시되지 않는다는 사실은 라벨의 위치가 실제로 상위 위치의 오프셋임을 고려하지 않았 음을 나타낼 수 있습니다. 이제 부모의 anchorPoint도 변경 한 경우 레이블의 위치는 부모의 왼쪽 하단 모서리에서 오프셋이됩니다.

짧은 이야기 : anchorPoint를 변경하지 않고 그대로 둡니다. 레이블을 제외하고는 anchorPoint를 사용하여 정렬에 영향을 줄 수 있습니다.

+0

확실히 여기서 뭔가가 일어나고 있습니다. 결국 아래의 방법을 사용하여이 작업을했습니다. – Echilon