objective-c - 如何获得左对齐的cocos2d菜单

标签 objective-c ios xcode cocos2d-iphone

因此,我希望主菜单具有ccMenu。我尝试使用align垂直和alignItemsInColumns进行探索,但是所有这些选项将菜单中的项目居中。如果我想要看起来像这样的东西:

标题

 Option 1 has a very long name
 Option 2 is short
 Shorter

代替这个:

标题
Option 1 has a very long name
     Option 2 is short
          Shorter

如何在cocos2d中实现呢?谢谢您的帮助。

编辑:

我尝试使用此代码,但得到了以下结果:
CCLabelTTF * sin = [CCLabelTTF labelWithString:@"Single Player" dimensions:CGSizeMake(120,    25) alignment:UITextAlignmentLeft fontName:font fontSize:20];
CCMenuItem * item1 = [CCMenuItemLabel itemWithLabel:sin target:self selector:@selector(goToSinglePlayer:)];

CCLabelTTF * spl = [CCLabelTTF labelWithString:@"Splitscreen" dimensions:CGSizeMake(100, 25) alignment:UITextAlignmentLeft fontName:font fontSize:20];
CCMenuItem * item2 = [CCMenuItemLabel itemWithLabel:spl target:self selector:@selector(goToSplitscreen:)];

CCLabelTTF * ach = [CCLabelTTF labelWithString:@"Achievements" dimensions:CGSizeMake(130, 25) alignment:UITextAlignmentLeft fontName:font fontSize:20];
CCMenuItem * item3 = [CCMenuItemLabel itemWithLabel:ach target:self selector:@selector(goToAchievements:)];

CCLabelTTF * str = [CCLabelTTF labelWithString:@"Store" dimensions:CGSizeMake(50, 25) alignment:UITextAlignmentLeft fontName:font fontSize:20];
CCMenuItem * item4 = [CCMenuItemLabel itemWithLabel:str target:self selector:@selector(goToStore:)];

CCLabelTTF * set = [CCLabelTTF labelWithString:@"Settings" dimensions:CGSizeMake(80, 25) alignment:UITextAlignmentLeft fontName:font fontSize:20];
CCMenuItem * item5 = [CCMenuItemLabel itemWithLabel:set target:self selector:@selector(goToSettings:)];

CCMenu * mainMenu = [CCMenu menuWithItems:item1, item2, item3, item4, item5, nil];

[mainMenu setColor:ccBLACK];
[mainMenu alignItemsVerticallyWithPadding:10];
mainMenu.position =  ccp(size.width/6, size.height/2);
[self addChild:mainMenu];

似乎它什么也没做。代码有什么问题?

最佳答案

用CCMenuItemLabel项目创建菜单。使用CCLabelTTF创建每个标签,并为其左对齐和已知尺寸。当您将menuItem添加到菜单中时,如果所有尺寸均相同,它们将对齐。

添加 :

 CGSize label = CGSizeMake(160., 45.); 
 CCLabelTTF *label1 = [CCLabelTTF labelWithString:@"Option1 for example" 
                                       dimensions:size 
                                        alignment:UITextAlignmentLeft
                                         fontName:@"pickYourOwnFont" fontSize:16.];

 CCLabelTTF *label2 = [CCLabelTTF labelWithString:@"Option2 below option 1" 
                                       dimensions:size 
                                        alignment:UITextAlignmentLeft 
                                         fontName:@"pickYourOwnFont" fontSize:16.];
CCMenuItem * item1 = [CCMenuItemLabel itemWithLabel:label1];
CCMenuItem * item2 = [CCMenuItemLabel itemWithLabel:label2];
CCMenu * leftAlignedMenu=[CCMenu menuWithItems:item1,item2,nil];

您可以为标签(颜色等)和menuItem(回调目标和选择器)设置任意属性。

关于objective-c - 如何获得左对齐的cocos2d菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10466870/

相关文章:

iphone - 来自另一层cocos2d的ccmenuitem

Xcode 7.1 与 iOS 9.1 错误 : include of non-modular header inside framework module 'Parse.PFGeoPoint'

objective-c - 如何跟踪 UIButton 被按下了多少次?

iphone - 通过模态视图呈现详细 View Controller

iphone - iOS WebTryThreadLock 崩溃

ios - AVPlayerViewController UITapGestureRecognizer 在 ios 11 中不起作用

ios - 将uiscrollview subview 调整为全屏

objective-c - 什么是 dispatch_group_create 的 Swift 等价物

ios - 处理 UITableViewCell 中的数据对象

ios - 在构建时更改子程序名称以避免 Xcode 中的冲突