基本操作
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; // 设置return按键的样式
UIView *keyBoard = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 300)];
keyBoard.backgroundColor = [UIColor greenColor];
// userNameTextField.inputView = keyBoard; // 替换键盘
[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;
}
时间: 2024-10-25 03:48:53