问题
When I'm in Safari in iOS, and I hit a form that requires I enter name and address, I get shortcuts in the keyboard area. For example, here is the keyboard when the focus is in a first name field. I can tap "Robert" instead of typing a name.
A similar thing happens for fields for last name, phone, email, zip code.
Can I get a similar thing in a native iOS app, in a UITextField
? Is there a way to mark a field so that the keyboard will offer these shortcut suggestions?
回答1:
You can turn on or off it by set autocorrectionType
of UITextField
. Default value of autocorrectionType
is UITextAutocorrectionTypeDefault
. It means you don't need to do anything, by default it will be showed.
UITextAutocorrectionTypeYes; // Turn on
UITextAutocorrectionTypeDefault; // Turn on
UITextAutocorrectionTypeNo; // Turn off
But beside of set autocorrectionType
value, you need to make sure Predictive
in Keyboards Setting
of device is turned on. If Predictive
is turned off, suggestion won't be displayed even you changed autocorrectionType
to UITextAutocorrectionTypeYes
.
UPDATE
You can change textContentType
to choose with type of suggestion will be shown. Suggestions are from the Contacts
. For example, you want phone suggestion
textField.textContentType = UITextContentTypeTelephoneNumber;
回答2:
One note, if you have email and password on a screen, sometimes the native keychain detection overrides textContentType=email so autofill using textContentType stops working for the email field. For things like "create account" where they don't likely have existing credentials in keychain it's best to disable the keychain. To fix this
To disable keychain, just do this: passwordTextField.textContentType = @"";
Then email autofill should work again:
emailTextField.textContentType = UITextContentTypeEmail;
回答3:
For iOS 13, I've found that email suggestions are only enabled if you set all 3 of the following properties:
emailTextField.textContentType = .emailAddress
emailTextField.keyboardType = .emailAddress
emailTextField.autocorrectionType = .yes
You can do it programatically or via storyboard.
来源:https://stackoverflow.com/questions/44312464/shortcuts-or-autofill-for-contact-info-in-uitextfields