cocos2d 使用UITextField


其实主要是两行代码:

  1. UITextField *inputTextField = [[UITextField alloc] initWithFrame:CGRectMake(50,50,140,30)];  
  2. [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我们是完全可以实现文本框的各种功能,在此我参考网上的一些例子最后写了一个例子和大家分享一下:

  1. // When you import this file, you import all the cocos2d classes  
  2. #import "cocos2d.h"  
  3.   
  4. // HelloWorldLayer  
  5. @interface HelloWorldLayer : CCLayerColor<UITextFieldDelegate> {  
  6.     UITextField *inputField;  
  7. }  
  8.   
  9. @property (nonatomic, retain) IBOutlet UITextField *inputField;  
  10.   
  11. // returns a CCScene that contains the HelloWorldLayer as the only child  
  12. +(CCScene *) scene;  
  13.   
  14. @end  

 

  1. // Import the interfaces  
  2. #import "HelloWorldLayer.h"  
  3.   
  4. // HelloWorldLayer implementation  
  5. @implementation HelloWorldLayer  
  6. @synthesize inputField;  
  7.   
  8. +(CCScene *) scene  
  9. {  
  10.     // 'scene' is an autorelease object.  
  11.     CCScene *scene = [CCScene node];  
  12.       
  13.     // 'layer' is an autorelease object.  
  14.     HelloWorldLayer *layer = [HelloWorldLayer node];  
  15.       
  16.     // add layer as a child to scene  
  17.     [scene addChild: layer];  
  18.       
  19.     // return the scene  
  20.     return scene;  
  21. }  
  22.   
  23. // on "init" you need to initialize your instance  
  24. -(id) init  
  25. {  
  26.     // always call "super" init  
  27.     // Apple recommends to re-assign "self" with the "super" return value  
  28.     if( (self=[super initWithColor:ccc4(0, 255, 255,255)])){  
  29.           
  30.         inputField = [[UITextField alloc] initWithFrame:CGRectMake(100,100,150,30)];//设置文本框大小和位置  
  31.         [inputField setBackgroundColor:[UIColor yellowColor]];//背景色  
  32.         [inputField setTextAlignment:UITextAlignmentCenter];//水平居中  
  33.         inputField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;//垂直居中  
  34.         [inputField setFont:[UIFont fontWithName:@"Bauhaus Md BT" size:22]];  
  35.         [inputField setText:@"CoreyGuo"];  
  36.         [inputField becomeFirstResponder];//弹出键盘   
  37.         //inputField.autocorrectionType = UITextAutocorrectionTypeNo;   
  38.         //inputField.autocapitalizationType = UITextAutocapitalizationTypeNone;   
  39.         //inputField.returnKeyType = UIReturnKeyDone;  
  40.         [inputField setTextColor:[UIColor colorWithRed:96/255.0f green:55/255.0f blue:17/255.0f alpha:1]];  
  41.         inputField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X  
  42.         //inputField.secureTextEntry = YES;  //如果是密码框时 ,加上这句  
  43.         inputField.delegate=self;  
  44.         [[[CCDirector sharedDirector] openGLView] addSubview:inputField];   
  45.           
  46.         self.isTouchEnabled=YES;  
  47.     }  
  48.     return self;  
  49. }  
  50.   
  51. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  
  52. {  
  53.     if ([textField.text length] == 8) { //字符限制  
  54.         if ([string length] < 1) {  //用于相应退格事件  
  55.             textField.text = [textField.text substringToIndex:[textField.text length] - 1];  //如果用户点击退格,那么将文本内容去一位  
  56.         }  
  57.         return FALSE;  //返回FALSE,文本框就不会相应键盘上的输入,包括退格  
  58.     }  
  59.     return TRUE;  
  60. }  
  61. - (void)textFieldDidEndEditing:(UITextField *)textField  
  62. {  
  63.     NSLog(@"textFieldDidEndEditing");//完成后要处理的事件  
  64. }  
  65.   
  66. -(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{  
  67.       
  68.     [inputField resignFirstResponder];//表示关闭键盘  
  69. }  
  70.   
  71. // on "dealloc" you need to release all your retained objects  
  72. - (void) dealloc  
  73. {  
  74.     // in case you have something to dealloc, do it in this method  
  75.     // in this particular example nothing needs to be released.  
  76.     // cocos2d will automatically release all the children (Label)  
  77.     [inputField removeFromSuperview];  
  78.     [inputField release];  
  79.       
  80.     // don't forget to call "super dealloc"  
  81.     [super dealloc];  
  82. }  
  83. @end  

相关内容