2011-09-04 85 views
157

是否有可能以編程方式更改的UITextField的鍵盤類型,所以這樣的事情是可能的:編程改變的UITextField鍵盤類型

if(user is prompted for numeric input only) 
    [textField setKeyboardType: @"Number Pad"]; 

if(user is prompted for alphanumeric input) 
    [textField setKeyboardType: @"Default"]; 
+3

我建議你將'doozy'這個詞改爲更常用的東西。請記住,SO是國際站點,不是北美站點 – abbood

回答

344

有一個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]; 
+0

哇。非常簡單。謝謝! –

+6

請注意,這不會阻止聰明/錯誤的用戶輸入其他字符。例如:如果表情符號鍵盤在*之前處於活動狀態*他們點擊您的號碼字段,他們可以自由地在其中輸入笑臉。對此,你無能爲力,這絕對是蘋果的錯誤,但你應該**確保你的代碼不會崩潰,如果你在數字字段中得到非數字。 –

+0

今天發現,這是'UITextField'採用的'UITextInputTraits'協議的屬性。 – rounak

23

是你可以,例如:

[textField setKeyboardType:UIKeyboardTypeNumberPad]; 
2

有一個屬性爲這keyboardType。 你想要做的是取代你的字符串@"Number Pad@"DefaultUIKeyboardTypeNumberPadUIKeyboardTypeDefault

你的新代碼應該是這個樣子:

if(user is prompted for numeric input only) 
    [textField setKeyboardType:UIKeyboardTypeNumberPad]; 

else if(user is prompted for alphanumeric input) 
    [textField setKeyboardType:UIKeyboardTypeDefault]; 

祝您好運!

5

,以使文本字段接受字母數字只設置該屬性

textField.keyboardType = UIKeyboardTypeNamePhonePad; 
59

值得一提的是,如果你想有一個當前重點場更新鍵盤立即輸入,還有一個額外的步驟:

// textField is set to a UIKeyboardType other than UIKeyboardTypeEmailAddress 

[textField setKeyboardType:UIKeyboardTypeEmailAddress]; 
[textField reloadInputViews]; 

沒有調用reloadInputViews,鍵盤不會改變,直到選定字段(first responder)失去和獲得焦點。

UIKeyboardTypecan be found here,或的完整列表:

typedef enum : NSInteger { 
    UIKeyboardTypeDefault, 
    UIKeyboardTypeASCIICapable, 
    UIKeyboardTypeNumbersAndPunctuation, 
    UIKeyboardTypeURL, 
    UIKeyboardTypeNumberPad, 
    UIKeyboardTypePhonePad, 
    UIKeyboardTypeNamePhonePad, 
    UIKeyboardTypeEmailAddress, 
    UIKeyboardTypeDecimalPad, 
    UIKeyboardTypeTwitter, 
    UIKeyboardTypeWebSearch, 
    UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable 
} UIKeyboardType; 
+0

這是有用的信息知道,我會建議做一個問答式自答問題,只是爲了提取有關當前重點字段輸入更改的信息(這就是我找到這個答案時尋找的) – Stonz2

+1

它也是值得一提的是,在當前未關注的文本字段上調用reloadInputViews不會立即改變鍵盤類型。所以最好先調用[textfield becomeFirstResponder],然後[textField reloadInputViews] – Qiulang

+0

它是'[textField reloadInputViews];'我錯過了。謝謝! –

1

誰想要使用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]]; 
} 
6
_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; 
6
textFieldView.keyboardType = UIKeyboardType.PhonePad 

這是迅速的。此外,在爲了這個正常工作,必須在​​

0

後設置這是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 
0

編程改變的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) 
}