且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在iOS8自定义键盘中使用自动更正和快捷列表?

更新时间:2023-02-03 09:21:08

实现词典看起来非常像这样:

Implementing the lexicon would look pretty much like this:


  1. 使用 requestSupplementaryLexiconWithCompletion()在启动时获取词典。

  2. 输入每种类型的文本将其添加到 NSString (跟踪当前单词)

  3. 当用户按空格时(结尾的单词) )检查词典中的词典

  4. 如果匹配c数量字符数并删除该字符数

  5. 输入词典建议的建议

  6. 清除字符串并重新开始

  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

此外,您还可以使用 UITextChecker 来提供更高级的自动更正功能。

Additionally you could also use UITextChecker to offer more advanced auto-correct features.

代码(在Objective-C中,这可能不是我在公共汽车上用SO写的100%准确但它应该这样做):

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.

来源:iOS 8键盘和UILexicon的个人体验

Source: Personal experience with iOS 8 keyboards and UILexicon