I want to use the autocorrection and shortcut list like default English keyboard with my custom keyboard. I check the in keyboard document but don't know how to use it.
In keyboard documentation.
Every custom keyboard (independent of the value of its RequestsOpenAccess key) has access to a basic autocorrection lexicon through the UILexicon class. Make use of this class, along with a lexicon of your own design, to provide suggestions and autocorrections as users are entering text. The UILexicon object contains words from various sources, including:
How to access shortcut list and input from our dictionary in Objective-C?
How to use UILexicon with requestSupplementaryLexiconWithCompletion?
Implementing the lexicon would look pretty much like this:
requestSupplementaryLexiconWithCompletion()
to get the lexicon upon launch once.NSString
(tracking the current word)Additionally you could also use UITextChecker
to offer more advanced auto-correct features.
Code (in Objective-C, this may not be 100% accurate I wrote in SO while on the bus but it should do):
UILexicon *lexicon;
NSString *currentString;
-(void)viewDidLoad {
[self requestSupplementaryLexiconWithCompletion:^(UILexicon *receivedLexicon) {
self.lexicon = receivedLexicon;
}];
}
-(IBAction)myTypingAction:(UIButton *)sender {
[documentProxy insertText:sender.title];
[currentString stringByAppendingString:sender.title];
}
-(IBAction)space {
[documentProxy insertText:@" "];
for (UILexiconEntry *lexiconEntry in lexicon.entries) {
if (lexiconEntry.userInput isEqualToString:currentString) {
for (int i = 0; currentString.length >=i ; i++) {
[documentProxy deleteTextBackwards];
}
[documentProxy insertText:lexiconEntry.documentText];
currentString = @"";
}
}
}
Feel free to comment if you have any more questions.
Source: Personal experience with iOS 8 keyboards and UILexicon
With regards to auto-correction, I was able to add it using link. Here's the code snippet I used from the link:
UITextChecker *checker = [[UITextChecker alloc] init];
NSRange checkRange = NSMakeRange(0, self.txView.text.length);
NSRange misspelledRange = [checker rangeOfMisspelledWordInString:self.txView.text
range:checkRange
startingAt:checkRange.location
wrap:NO
language:@"en_US"];
NSArray *arrGuessed = [checker guessesForWordRange:misspelledRange inString:self.txView.text language:@"en_US"];
self.txView.text = [self.txView.text stringByReplacingCharactersInRange:misspelledRange
withString:[arrGuessed objectAtIndex:0]];
The full documentation from Apple can be found here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With