uitextfield 键盘类型_iphone - 以编程方式更改UITextField键盘类型

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"];

11个解决方案

356 votes

有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];

PengOne answered 2019-04-17T13:39:37Z

67 votes

值得注意的是,如果你想要一个当前关注的字段立即更新键盘类型,还有一个额外的步骤:

// textField is set to a UIKeyboardType other than UIKeyboardTypeEmailAddress

[textField setKeyboardType:UIKeyboardTypeEmailAddress];

[textField reloadInputViews];

如果没有调用UIKeyboardType,键盘将不会更改,直到所选字段(第一个响应者)丢失并重新获得焦点。

可以在此处找到UIKeyboardType值的完整列表,或者:

typedef enum : NSInteger {

UIKeyboardTypeDefault,

UIKeyboardTypeASCIICapable,

UIKeyboardTypeNumbersAndPunctuation,

UIKeyboardTypeURL,

UIKeyboardTypeNumberPad,

UIKeyboardTypePhonePad,

UIKeyboardTypeNamePhonePad,

UIKeyboardTypeEmailAddress,

UIKeyboardTypeDecimalPad,

UIKeyboardTypeTwitter,

UIKeyboardTypeWebSearch,

UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable

} UIKeyboardType;

jterry answered 2019-04-17T13:40:24Z

23 votes

是的,你可以,例如:

[textField setKeyboardType:UIKeyboardTypeNumberPad];

Alan Moore answered 2019-04-17T13:40:50Z

8 votes

textFieldView.keyboardType = UIKeyboardType.PhonePad

这是为了迅速。 此外,为了使其正常工作,必须在textFieldView.delegate = self之后设置

fonz answered 2019-04-17T13:41:17Z

6 votes

要使文本字段接受字母数字,只设置此属性

textField.keyboardType = UIKeyboardTypeNamePhonePad;

Hossam Ghareeb answered 2019-04-17T13:41:51Z

6 votes

_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;

Kishor Kumar Rawat answered 2019-04-17T13:42:11Z

2 votes

有一个属性叫做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];

祝好运!

Kyle Rosenbluth answered 2019-04-17T13:42:54Z

1 votes

对于想要使用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]];

}

Brian answered 2019-04-17T13:43:23Z

1 votes

这是Swift 3的UIKeyboardTypes:

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

pableiros answered 2019-04-17T13:44:03Z

1 votes

斯威夫特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 work

} else {

textField.keyboardType = .default

textField.reloadInputViews()

}

Sanket Ray answered 2019-04-17T13:44:37Z

0 votes

以编程方式更改UITextField键盘类型swift 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)

}

Tanjim3 answered 2019-04-17T13:45:08Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值