其實主要是兩行代碼:
- UITextField *inputTextField = [[UITextField alloc] initWithFrame:CGRectMake(50,50,140,30)];
- [CCDirector sharedDirector] openGLView] addSubview:inputTextField];
但是一定要注意以下兩點事項:
1、文本框默認樣式是直線型,邊框顏色是黑色;
2、文本框默認背景是透明的;
3、UITextField的坐標系與cocos2d是不一樣的;
所以有很多童鞋說加不上去,其實是已經加上去了,只是你看不到而已,要麼是你黑背景,要麼是坐標的問題。另外關於旋轉問題我沒有深入研究,但是主要注意一下兩點就問題不大了:
代碼1:[[[[CCDirector sharedDirector] openGLView] window] addSubview:inputTextField];
效果:添加的inputTextField會隨著window是橫屏還是豎屏變化,添加之後如果view下面覆蓋了一個cocos2d的按鈕,點擊按鈕的區域按鈕不響應點擊。
代碼2:[[[CCDirector sharedDirector] openGLView] addSubview:inputTextField];
效果:不隨著變化,並且底部的按鈕相應點擊
其實在cocos2d中通過文本框加上之後,通過UITextFieldDelegate我們是完全可以實現文本框的各種功能,在此我參考網上的一些例子最後寫了一個例子和大家分享一下:
- // When you import this file, you import all the cocos2d classes
- #import "cocos2d.h"
-
- // HelloWorldLayer
- @interface HelloWorldLayer : CCLayerColor<UITextFieldDelegate> {
- UITextField *inputField;
- }
-
- @property (nonatomic, retain) IBOutlet UITextField *inputField;
-
- // returns a CCScene that contains the HelloWorldLayer as the only child
- +(CCScene *) scene;
-
- @end
- // Import the interfaces
- #import "HelloWorldLayer.h"
-
- // HelloWorldLayer implementation
- @implementation HelloWorldLayer
- @synthesize inputField;
-
- +(CCScene *) scene
- {
- // 'scene' is an autorelease object.
- CCScene *scene = [CCScene node];
-
- // 'layer' is an autorelease object.
- HelloWorldLayer *layer = [HelloWorldLayer node];
-
- // add layer as a child to scene
- [scene addChild: layer];
-
- // return the scene
- return scene;
- }
-
- // on "init" you need to initialize your instance
- -(id) init
- {
- // always call "super" init
- // Apple recommends to re-assign "self" with the "super" return value
- if( (self=[super initWithColor:ccc4(0, 255, 255,255)])){
-
- inputField = [[UITextField alloc] initWithFrame:CGRectMake(100,100,150,30)];//設置文本框大小和位置
- [inputField setBackgroundColor:[UIColor yellowColor]];//背景色
- [inputField setTextAlignment:UITextAlignmentCenter];//水平居中
- inputField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;//垂直居中
- [inputField setFont:[UIFont fontWithName:@"Bauhaus Md BT" size:22]];
- [inputField setText:@"CoreyGuo"];
- [inputField becomeFirstResponder];//彈出鍵盤
- //inputField.autocorrectionType = UITextAutocorrectionTypeNo;
- //inputField.autocapitalizationType = UITextAutocapitalizationTypeNone;
- //inputField.returnKeyType = UIReturnKeyDone;
- [inputField setTextColor:[UIColor colorWithRed:96/255.0f green:55/255.0f blue:17/255.0f alpha:1]];
- inputField.clearButtonMode = UITextFieldViewModeWhileEditing; //編輯時會出現個修改X
- //inputField.secureTextEntry = YES; //如果是密碼框時 ,加上這句
- inputField.delegate=self;
- [[[CCDirector sharedDirector] openGLView] addSubview:inputField];
-
- self.isTouchEnabled=YES;
- }
- return self;
- }
-
- - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
- {
- if ([textField.text length] == 8) { //字符限制
- if ([string length] < 1) { //用於相應退格事件
- textField.text = [textField.text substringToIndex:[textField.text length] - 1]; //如果用戶點擊退格,那麼將文本內容去一位
- }
- return FALSE; //返回FALSE,文本框就不會相應鍵盤上的輸入,包括退格
- }
- return TRUE;
- }
- - (void)textFieldDidEndEditing:(UITextField *)textField
- {
- NSLog(@"textFieldDidEndEditing");//完成後要處理的事件
- }
-
- -(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
-
- [inputField resignFirstResponder];//表示關閉鍵盤
- }
-
- // on "dealloc" you need to release all your retained objects
- - (void) dealloc
- {
- // in case you have something to dealloc, do it in this method
- // in this particular example nothing needs to be released.
- // cocos2d will automatically release all the children (Label)
- [inputField removeFromSuperview];
- [inputField release];
-
- // don't forget to call "super dealloc"
- [super dealloc];
- }
- @end