In .h file:
@interface SceneName : CCLayer <UITextFieldDelegate>
UITextField *nameField;
In .m file:
// To create UITextField and assigning properties//
-(void)createTextField{
// create a UIView here(say profileView). Then do the following code //
nameField = [[UITextField alloc] initWithFrame:CGRectMake(5, 20, 150, 30)];
nameField.center = CGPointMake(285, 120);
nameField.backgroundColor = [UIColor whiteColor];
nameField.textAlignment = UITextAlignmentCenter;
nameField.delegate = self;
[nameField.layer setBorderColor:[[UIColor redColor]CGColor]];
[nameField.layer setBorderWidth:1];
nameField.layer.cornerRadius = 8;
nameField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[nameField addTarget:self action:@selector(valueChanged)
forControlEvents:UIControlEventEditingChanged];
[profileView addSubview:nameField];
}
// To resign first responder/keyboard //
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
NSLog(@"%@ ",textField.text); // To get/print the data from the TextField
return YES;
}
-(void)valueChanged{
// To Limit text field text length //
if ([nameField.text length] > 12) {
nameField.text = [nameField.text substringToIndex:12];
}
}
/*---------Some other important UITextField delegates---------*/
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return YES;
}
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}
- (void) textFieldDidBeginEditing:(UITextField *)textField
{
}
@interface SceneName : CCLayer <UITextFieldDelegate>
UITextField *nameField;
In .m file:
// To create UITextField and assigning properties//
-(void)createTextField{
// create a UIView here(say profileView). Then do the following code //
nameField = [[UITextField alloc] initWithFrame:CGRectMake(5, 20, 150, 30)];
nameField.center = CGPointMake(285, 120);
nameField.backgroundColor = [UIColor whiteColor];
nameField.textAlignment = UITextAlignmentCenter;
nameField.delegate = self;
[nameField.layer setBorderColor:[[UIColor redColor]CGColor]];
[nameField.layer setBorderWidth:1];
nameField.layer.cornerRadius = 8;
nameField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[nameField addTarget:self action:@selector(valueChanged)
forControlEvents:UIControlEventEditingChanged];
[profileView addSubview:nameField];
}
// To resign first responder/keyboard //
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
NSLog(@"%@ ",textField.text); // To get/print the data from the TextField
return YES;
}
-(void)valueChanged{
// To Limit text field text length //
if ([nameField.text length] > 12) {
nameField.text = [nameField.text substringToIndex:12];
}
}
/*---------Some other important UITextField delegates---------*/
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return YES;
}
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}
- (void) textFieldDidBeginEditing:(UITextField *)textField
{
}
No comments:
Post a Comment