基本操作
UITextField *userNameTextField = [[UITextField alloc] init];
userNameTextField.frame = CGRectMake(30, 100, 220, 50);
[self.window addSubview:userNameTextField];
[userNameTextField release];
userNameTextField.borderStyle = UITextBorderStyleRoundedRect;
userNameTextField.placeholder = @"Enter your name";
userNameTextField.text = @"OUTLAN";
userNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
UILabel *leftLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
leftLable.text = @"N";
userNameTextField.leftView = leftLable;
userNameTextField.leftViewMode = UITextFieldViewModeAlways;
[leftLable release];
userNameTextField.enabled = YES;
userNameTextField.clearsOnBeginEditing = NO;
userNameTextField.secureTextEntry = NO;
userNameTextField.keyboardAppearance = UIKeyboardAppearanceDark;
userNameTextField.keyboardType = UIKeyboardTypeEmailAddress;
userNameTextField.returnKeyType = UIReturnKeySearch;
UIView *keyBoard = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 300)];
keyBoard.backgroundColor = [UIColor greenColor];
[keyBoard release];
UIView *inputAccessView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 20)];
inputAccessView.backgroundColor = [UIColor yellowColor];
userNameTextField.inputAccessoryView = inputAccessView;
[inputAccessView release];
收回键盘
设置代理对象。通常为self
textFiled.delegate = self;
当前类遵守协议
@interface AppDelegate : UIResponder <UIApplicationDelegate, UITextFieldDelegate>
实现协议方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"点击了Return");
[textField resignFirstResponder];
return YES;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
NSLog(@"begining");
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
NSLog(@"ending");
return YES;
}