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

后端 未结 9 954
慢半拍i
慢半拍i 2020-12-07 17:22

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.

9条回答
  •  离开以前
    2020-12-07 17:33

    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

提交回复
热议问题