问题
I have a UITextfield
, and as the user enters some text I need the text field to suggest some words from an array.
I have looked all over and it seems everyone is creating a table or something to display the suggestions.
Is there a way I can customise the default iOS autocompletion to use my source for the suggestions?
回答1:
You can't override the "default" spell checking autocompletion in UITextField, because the spell checking autocompletion is intended to correct errors and is handled by the OS itself (you can, however, disable it).
The way that most autocompletion systems work in iOS is using the very helpful UISearchDisplayController. You don't need a lot of code to get this working correctly, but it does require some screen real estate for showing a list of responses (like Google searches in Safari).
Apple has a decent bit of sameple code to show how to do this. It's available here.
回答2:
You can user Searchbar instead of your textfield and then implements its delegate method to give suggestion.Implement the Delegate Method
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
//your logic
}
回答3:
I've never actually used it, but you could check out the UITextChecker object. It's for spell checking text and completing parts of words. There are methods for adding/learning and ignoring words.
来源:https://stackoverflow.com/questions/10578079/change-the-spellcheck-autocompletion-source-for-a-uitextfield