[iphone] 프로그래밍 방식으로 UITextField 키보드 유형 변경
프로그래밍 방식으로 uitextfield의 키보드 유형을 변경하여 다음과 같은 것이 가능합니까?
if(user is prompted for numeric input only)
[textField setKeyboardType: @"Number Pad"];
if(user is prompted for alphanumeric input)
[textField setKeyboardType: @"Default"];
답변
에 대한 keyboardType
속성이 있습니다 UITextField
:
typedef enum {
UIKeyboardTypeDefault, // Default type for the current input method.
UIKeyboardTypeASCIICapable, // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active
UIKeyboardTypeNumbersAndPunctuation, // Numbers and assorted punctuation.
UIKeyboardTypeURL, // A type optimized for URL entry (shows . / .com prominently).
UIKeyboardTypeNumberPad, // A number pad (0-9). Suitable for PIN entry.
UIKeyboardTypePhonePad, // A phone pad (1-9, *, 0, #, with letters under the numbers).
UIKeyboardTypeNamePhonePad, // A type optimized for entering a person's name or phone number.
UIKeyboardTypeEmailAddress, // A type optimized for multiple email address entry (shows space @ . prominently).
UIKeyboardTypeDecimalPad, // A number pad including a decimal point
UIKeyboardTypeTwitter, // Optimized for entering Twitter messages (shows # and @)
UIKeyboardTypeWebSearch, // Optimized for URL and search term entry (shows space and .)
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated
} UIKeyboardType;
코드를 읽어야합니다
if(user is prompted for numeric input only)
[textField setKeyboardType:UIKeyboardTypeNumberPad];
if(user is prompted for alphanumeric input)
[textField setKeyboardType:UIKeyboardTypeDefault];
답변
당신이 원한다면 것을주의 그것의 가치가 현재에 초점을 맞춘 바로 키보드 유형을 업데이트 할 필드를 하나 추가 단계가있다 :
// textField is set to a UIKeyboardType other than UIKeyboardTypeEmailAddress
[textField setKeyboardType:UIKeyboardTypeEmailAddress];
[textField reloadInputViews];
에 대한 호출이 없으면 reloadInputViews
선택한 필드 ( 첫 번째 응답기 )가 포커스를 잃고 다시 얻을 때까지 키보드가 변경되지 않습니다 .
전체 UIKeyboardType
값 목록은 여기 또는 에서 찾을 수 있습니다 .
typedef enum : NSInteger {
UIKeyboardTypeDefault,
UIKeyboardTypeASCIICapable,
UIKeyboardTypeNumbersAndPunctuation,
UIKeyboardTypeURL,
UIKeyboardTypeNumberPad,
UIKeyboardTypePhonePad,
UIKeyboardTypeNamePhonePad,
UIKeyboardTypeEmailAddress,
UIKeyboardTypeDecimalPad,
UIKeyboardTypeTwitter,
UIKeyboardTypeWebSearch,
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable
} UIKeyboardType;
답변
예, 예를 들면 다음과 같습니다.
[textField setKeyboardType:UIKeyboardTypeNumberPad];
답변
textFieldView.keyboardType = UIKeyboardType.PhonePad
이것은 신속합니다. 또한이 기능이 제대로 작동하려면 다음에 설정해야합니다.textFieldView.delegate = self
답변
텍스트 필드에 영숫자 만 허용하게하려면이 속성을 설정하십시오.
textField.keyboardType = UIKeyboardTypeNamePhonePad;
답변
_textField .keyboardType = UIKeyboardTypeAlphabet;
_textField .keyboardType = UIKeyboardTypeASCIICapable;
_textField .keyboardType = UIKeyboardTypeDecimalPad;
_textField .keyboardType = UIKeyboardTypeDefault;
_textField .keyboardType = UIKeyboardTypeEmailAddress;
_textField .keyboardType = UIKeyboardTypeNamePhonePad;
_textField .keyboardType = UIKeyboardTypeNumberPad;
_textField .keyboardType = UIKeyboardTypeNumbersAndPunctuation;
_textField .keyboardType = UIKeyboardTypePhonePad;
_textField .keyboardType = UIKeyboardTypeTwitter;
_textField .keyboardType = UIKeyboardTypeURL;
_textField .keyboardType = UIKeyboardTypeWebSearch;
답변
스위프트 4
조건이 충족 될 때 키보드 유형을 변경하려는 경우 다음을 수행하십시오. 예를 들어 , 텍스트 필드의 수가 4 또는 5 일 때 키보드 유형을 기본값 에서 숫자 패드 로 변경하려면 다음을 수행하십시오.
textField.addTarget(self, action: #selector(handleTextChange), for: .editingChanged)
@objc func handleTextChange(_ textChange: UITextField) {
if textField.text?.count == 4 || textField.text?.count == 5 {
textField.keyboardType = .numberPad
textField.reloadInputViews() // need to reload the input view for this to work
} else {
textField.keyboardType = .default
textField.reloadInputViews()
}