gpt4 book ai didi

iphone - CCLabelTTF 垂直对齐 (Cocos2D)

转载 作者:行者123 更新时间:2023-11-29 13:38:11 24 4
gpt4 key购买 nike

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];

所以我创建了上面的标签并将其添加到我的图层中。由于 dimensions 参数,文本确实可以正确换行,但只有 第一行 文本垂直居中(例如,只有第一行文本恰好有 50%半屏标记上方字符高度的 50%,以及半屏标记下方字符高度的 50%:

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

其中 ----- 表示 1/2 屏幕(忽略点,它们只是用于 stackoverflow 格式)。我如何让它动态地将文本居中放置在它自己的中心?因此,例如在这种情况下,我希望“以我为中心”成为居中的。如果我的字符串只包含“垂直”,我希望它居中,等等....

最佳答案

尝试在第一个字符串之后写:

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

第二种可能的方案是根据标签boundingBox的大小,将其位置直接设置在upper

关于iphone - CCLabelTTF 垂直对齐 (Cocos2D),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9952421/

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