2011-09-04 100 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) 
}