gpt4 book ai didi

ios - CCLabelTTF 文本对齐

转载 作者:行者123 更新时间:2023-12-01 17:59:43 24 4
gpt4 key购买 nike

我有一个模仿按钮的类。它包含一个标签,我试图将其居中对齐(水平)。无论我尝试什么,标签都停留在左侧,让我认为这比看起来更复杂。这是唯一的初始化/设置代码:

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

最佳答案

我删除了标签的对齐属性并手动对齐它。显然,我看不到的事情正在发生,也许正如你所说@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);

关于ios - CCLabelTTF 文本对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12020656/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com