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

时间:2023-01-30
本文介绍了如何在 iOS8 自定义键盘中使用自动更正和快捷键列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想在我的自定义键盘上使用自动更正和快捷方式列表,例如默认英文键盘.我检查了输入键盘文档,但不知道如何使用它.

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.

在 键盘文档.

每个自定义键盘(与其 RequestsOpenAccess 键的值无关)都可以通过 UILexicon 类访问基本的自动更正词典.利用这个类以及您自己设计的词典,在用户输入文本时提供建议和自动更正.UILexicon 对象包含来自各种来源的单词,包括:

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:

  • 用户通讯簿数据库中未配对的名字和姓氏
  • 在设置">通用">键盘">快捷键"列表中定义的文本快捷键
  • 一本常用词词典

如何在 Objective-C 中从我们的字典中访问快捷方式列表和输入?

如何将 UILexicon 与 requestSupplementaryLexiconWithCompletion 一起使用?

推荐答案

实现词典看起来很像这样:

Implementing the lexicon would look pretty much like this:

  1. 使用 requestSupplementaryLexiconWithCompletion() 在启动时获取词典一次.
  2. 输入每个类型的文本将其添加到 NSString(跟踪当前单词)
  3. 当用户按下空格(当前单词的结尾)时,对照词典检查字符串
  4. 如果匹配,则计算字符数并删除该字符数
  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 的个人体验

这篇关于如何在 iOS8 自定义键盘中使用自动更正和快捷键列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如果键盘打开,则隐藏部分 activity_main.xml (Android) 下一篇:屏幕显示时如何删除字段的自动对焦/键盘弹出?

相关文章

最新文章