是否有可能以編程方式更改的UITextField的鍵盤類型,所以這樣的事情是可能的:編程改變的UITextField鍵盤類型
if(user is prompted for numeric input only)
[textField setKeyboardType: @"Number Pad"];
if(user is prompted for alphanumeric input)
[textField setKeyboardType: @"Default"];
是否有可能以編程方式更改的UITextField的鍵盤類型,所以這樣的事情是可能的:編程改變的UITextField鍵盤類型
if(user is prompted for numeric input only)
[textField setKeyboardType: @"Number Pad"];
if(user is prompted for alphanumeric input)
[textField setKeyboardType: @"Default"];
有一個UITextField
一個keyboardType
屬性:
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];
哇。非常簡單。謝謝! –
請注意,這不會阻止聰明/錯誤的用戶輸入其他字符。例如:如果表情符號鍵盤在*之前處於活動狀態*他們點擊您的號碼字段,他們可以自由地在其中輸入笑臉。對此,你無能爲力,這絕對是蘋果的錯誤,但你應該**確保你的代碼不會崩潰,如果你在數字字段中得到非數字。 –
今天發現,這是'UITextField'採用的'UITextInputTraits'協議的屬性。 – rounak
是你可以,例如:
[textField setKeyboardType:UIKeyboardTypeNumberPad];
有一個屬性爲這keyboardType
。 你想要做的是取代你的字符串@"Number Pad
和@"Default
與UIKeyboardTypeNumberPad
和UIKeyboardTypeDefault
。
你的新代碼應該是這個樣子:
if(user is prompted for numeric input only)
[textField setKeyboardType:UIKeyboardTypeNumberPad];
else if(user is prompted for alphanumeric input)
[textField setKeyboardType:UIKeyboardTypeDefault];
祝您好運!
,以使文本字段接受字母數字只設置該屬性
textField.keyboardType = UIKeyboardTypeNamePhonePad;
值得一提的是,如果你想有一個當前重點場更新鍵盤立即輸入,還有一個額外的步驟:
// textField is set to a UIKeyboardType other than UIKeyboardTypeEmailAddress
[textField setKeyboardType:UIKeyboardTypeEmailAddress];
[textField reloadInputViews];
沒有調用reloadInputViews
,鍵盤不會改變,直到選定字段(first responder)失去和獲得焦點。
的UIKeyboardType
值can be found here,或的完整列表:
typedef enum : NSInteger {
UIKeyboardTypeDefault,
UIKeyboardTypeASCIICapable,
UIKeyboardTypeNumbersAndPunctuation,
UIKeyboardTypeURL,
UIKeyboardTypeNumberPad,
UIKeyboardTypePhonePad,
UIKeyboardTypeNamePhonePad,
UIKeyboardTypeEmailAddress,
UIKeyboardTypeDecimalPad,
UIKeyboardTypeTwitter,
UIKeyboardTypeWebSearch,
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable
} UIKeyboardType;
誰想要使用UIDatePicker
作爲輸人:
UIDatePicker *timePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 0, 0)];
[timePicker addTarget:self action:@selector(pickerChanged:)
forControlEvents:UIControlEventValueChanged];
[_textField setInputView:timePicker];
// pickerChanged:
- (void)pickerChanged:(id)sender {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"d/M/Y"];
_textField.text = [formatter stringFromDate:[sender date]];
}
_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;
textFieldView.keyboardType = UIKeyboardType.PhonePad
這是迅速的。此外,在爲了這個正常工作,必須在
後設置這是UIKeyboardTypes
爲雨燕3:
public enum UIKeyboardType : Int {
case `default` // Default type for the current input method.
case asciiCapable // Displays a keyboard which can enter ASCII characters
case numbersAndPunctuation // Numbers and assorted punctuation.
case URL // A type optimized for URL entry (shows ./.com prominently).
case numberPad // A number pad with locale-appropriate digits (0-9, ۰-۹, ०-९, etc.). Suitable for PIN entry.
case phonePad // A phone pad (1-9, *, 0, #, with letters under the numbers).
case namePhonePad // A type optimized for entering a person's name or phone number.
case emailAddress // A type optimized for multiple email address entry (shows space @ . prominently).
@available(iOS 4.1, *)
case decimalPad // A number pad with a decimal point.
@available(iOS 5.0, *)
case twitter // A type optimized for twitter text entry (easy access to @ #)
@available(iOS 7.0, *)
case webSearch // A default keyboard type with URL-oriented addition (shows space . prominently).
@available(iOS 10.0, *)
case asciiCapableNumberPad // A number pad (0-9) that will always be ASCII digits.
public static var alphabet: UIKeyboardType { get } // Deprecated
}
這是從列表中使用鍵盤類型的例子:
textField.keyboardType = .numberPad
編程改變的UITextField鍵盤類型迅速3.0
lazy var textFieldTF: UITextField = {
let textField = UITextField()
textField.placeholder = "Name"
textField.frame = CGRect(x:38, y: 100, width: 244, height: 30)
textField.textAlignment = .center
textField.borderStyle = UITextBorderStyle.roundedRect
textField.keyboardType = UIKeyboardType.default //keyboard type
textField.delegate = self
return textField
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(textFieldTF)
}
我建議你將'doozy'這個詞改爲更常用的東西。請記住,SO是國際站點,不是北美站點 – abbood