为什么使用不同的输入类型?
键盘输入的类型帮助我们从用户获取必需的输入。
它移除不需要的键,并包括所需的部分。用户可以通过使用 UITextField 的键盘属性设置输入的类型。
- 如:文本字段( textField)。 keyboardType = UIKeyboardTypeDefault
键盘输入类型
输入的类型 | 描述 |
---|---|
UIKeyboardTypeASCIICapable | 键盘包括所有标准的 ASCII 字符。 |
UIKeyboardTypeNumbersAndPunctuation | 键盘显示数字和标点。 |
UIKeyboardTypeURL | 键盘的 URL 项优化。 |
UIKeyboardTypeNumberPad | 键盘用于 PIN 输入和显示一个数字键盘。 |
UIKeyboardTypePhonePad | 键盘对输入电话号码进行了优化。 |
UIKeyboardTypeNamePhonePad | 键盘用于输入姓名或电话号码。 |
UIKeyboardTypeEmailAddress | 键盘对输入电子邮件地址的优化。 |
UIKeyboardTypeDecimalPad | 键盘用来输入十进制数字。 |
UIKeyboardTypeTwitter | 键盘对 twitter @ 和 # 符号进行了优化。 |
添加自定义方法 addTextFieldWithDifferentKeyboard
-(void) addTextFieldWithDifferentKeyboard{ UITextField *textField1= [[UITextField alloc]initWithFrame: CGRectMake(20, 50, 280, 30)]; textField1.delegate = self; textField1.borderStyle = UITextBorderStyleRoundedRect; textField1.placeholder = @"Default Keyboard"; [self.view addSubview:textField1]; UITextField *textField2 = [[UITextField alloc]initWithFrame: CGRectMake(20, 100, 280, 30)]; textField2.delegate = self; textField2.borderStyle = UITextBorderStyleRoundedRect; textField2.keyboardType = UIKeyboardTypeASCIICapable; textField2.placeholder = @"ASCII keyboard"; [self.view addSubview:textField2]; UITextField *textField3 = [[UITextField alloc]initWithFrame: CGRectMake(20, 150, 280, 30)]; textField3.delegate = self; textField3.borderStyle = UITextBorderStyleRoundedRect; textField3.keyboardType = UIKeyboardTypePhonePad; textField3.placeholder = @"Phone pad keyboard"; [self.view addSubview:textField3]; UITextField *textField4 = [[UITextField alloc]initWithFrame: CGRectMake(20, 200, 280, 30)]; textField4.delegate = self; textField4.borderStyle = UITextBorderStyleRoundedRect; textField4.keyboardType = UIKeyboardTypeDecimalPad; textField4.placeholder = @"Decimal pad keyboard"; [self.view addSubview:textField4]; UITextField *textField5= [[UITextField alloc]initWithFrame: CGRectMake(20, 250, 280, 30)]; textField5.delegate = self; textField5.borderStyle = UITextBorderStyleRoundedRect; textField5.keyboardType = UIKeyboardTypeEmailAddress; textField5.placeholder = @"Email keyboard"; [self.view addSubview:textField5]; UITextField *textField6= [[UITextField alloc]initWithFrame: CGRectMake(20, 300, 280, 30)]; textField6.delegate = self; textField6.borderStyle = UITextBorderStyleRoundedRect; textField6.keyboardType = UIKeyboardTypeURL; textField6.placeholder = @"URL keyboard"; [self.view addSubview:textField6]; }
在 ViewController.m 中更新 viewDidLoad,如下所示
(void)viewDidLoad { [super viewDidLoad]; //The custom method to create textfield with different keyboard input [self addTextFieldWithDifferentKeyboard]; //Do any additional setup after loading the view, typically from a nib }
输出
现在当我们运行应用程序时我们就会得到下面的输出:
选择不同的文本区域我们将看到不同的键盘。
点我分享笔记