Implementing the lexicon would look pretty much like this:
- Use
requestSupplementaryLexiconWithCompletion()
to get the lexicon upon launch once.
- Each type text is inputted add it to a
NSString
(tracking the current word)
- When user presses space (end of curent word) check the string against the lexicon
- If it's a match count the number of characters and delete that number of characters
- Input the suggestion suggested by the lexicon
- Clear the string and start again
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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…