iOS_UITextField 基本操作
基本操作
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
實現協議方法
- (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;}