歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> cocos2d 使用UITextField

cocos2d 使用UITextField

日期:2017/3/1 10:13:17   编辑:Linux編程

其實主要是兩行代碼:

  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. // HelloWorldLayer
  4. @interface HelloWorldLayer : CCLayerColor<UITextFieldDelegate> {
  5. UITextField *inputField;
  6. }
  7. @property (nonatomic, retain) IBOutlet UITextField *inputField;
  8. // returns a CCScene that contains the HelloWorldLayer as the only child
  9. +(CCScene *) scene;
  10. @end

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