其实主要是两行代码:
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