Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use autocorrection and shortcut list in iOS8 custom keyboard?

Tags:

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:

  • Unpaired first names and last names from the user’s Address Book database
  • Text shortcuts defined in the Settings > General > Keyboard > Shortcuts list
  • A common words dictionary

How to access shortcut list and input from our dictionary in Objective-C?

How to use UILexicon with requestSupplementaryLexiconWithCompletion?

like image 890
LE SANG Avatar asked Jul 08 '14 08:07

LE SANG


2 Answers

Implementing the lexicon would look pretty much like this:

  1. Use requestSupplementaryLexiconWithCompletion() to get the lexicon upon launch once.
  2. Each type text is inputted add it to a NSString (tracking the current word)
  3. When user presses space (end of curent word) check the string against the lexicon
  4. If it's a match count the number of characters and delete that number of characters
  5. Input the suggestion suggested by the lexicon
  6. 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

like image 55
donkey Avatar answered Nov 15 '22 12:11

donkey


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.

like image 32
jaytrixz Avatar answered Nov 15 '22 13:11

jaytrixz