How to Change Uitextfield Keyboard Type to Email in Swift

How to change UITextField keyboard type to email in Swift

Try this :

Swift 3

self.promoTextField.keyboardType = UIKeyboardType.emailAddress
// Or Shorter version
self.promoTextField.keyboardType = .emailAddress

Swift < 3

self.promoTextField.keyboardType = UIKeyboardType.EmailAddress

Programmatically change UITextField Keyboard type

There is a keyboardType property for a 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;

Your code should read

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

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

Change keyboard type of a text field in Swift during runtime

Just call reloadInputViews() on your UITextField after you change keyboard's type


textField.keyboardType = .numberPad
textField.reloadInputViews()

How to set Keyboard type of TextField in SwiftUI?

To create a field where you can enter secure text you can use SecureField($password)

https://developer.apple.com/documentation/swiftui/securefield

If you want to set the contentType of an textField to eg. .oneTimeCode you can do it like this. Also works with keyboardType

TextField($code)
.textContentType(.oneTimeCode)
.keyboardType(.numberPad)

Change keyboard input type programmatically (Swift)

Here is a solution from https://www.tutorialspoint.com/how-to-restrict-uitextfield-to-take-only-numbers-in-swift

import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var tf: UITextField!
override func viewDidLoad() {
tf.delegate = self
super.viewDidLoad()
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let x = string.rangeOfCharacter(from: NSCharacterSet.decimalDigits) {
return true
} else {
return false
}
}
}

swift how to suggest user email in uitextfield as autocomplete

Set textContentType to emailAddress

let textField = UITextField()
textField.textContentType = .emailAddress


Related Topics



Leave a reply



Submit