简体   繁体   English

CCLabelTTF垂直对齐(Cocos2D)

[英]CCLabelTTF Vertically Align (Cocos2D)

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

So I create the above label and add it to my layer. 因此,我创建了上面的标签并将其添加到我的图层中。 Due to the dimensions parameter, the text does wrap correctly, but only the very first line of text is centered vertically (eg only the very first line of text has exactly 50% of the character height above the half screen mark, and exactly 50% of the character height beneath the half screen mark: 由于使用了dimensions参数,文本确实可以正确换行,但是只有文本的第一行垂直居中(例如,仅文本的第一行的字符高度恰好是半屏标记的50%,正好是50%半屏标记下方的字符高度:

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

Where the ----- denotes 1/2 screen (ignore the dots, they are just there for stackoverflow formatting). -----表示1/2屏幕(忽略点,它们只是用于stackoverflow格式化)。 How do I get it to dynamically center the text about whatever the center of itself? 如何获得将文本动态居中放置在中心位置的信息? So for example in this case, I would want 'Center Me' to be what is centered. 因此,例如在这种情况下,我希望“居中”成为居中位置。 If my string only contained 'Vertically' I would expect that to be centered, etc.... 如果我的字符串仅包含“垂直”,我希望它居中,等等。

Try to write after the first string: 尝试在第一个字符串之后写:

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

The second possible solution is to set its position directly upper according to the label boundingBox size 第二种可能的解决方案是根据标签boundingBox的大小将其位置直接设置在上方

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM