问题描述
我想在我的自定义键盘上使用自动更正和快捷方式列表,例如默认英文键盘.我检查了输入键盘文档,但不知道如何使用它.
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:
- 使用 requestSupplementaryLexiconWithCompletion() 在启动时获取词典一次.
- 输入每个类型的文本将其添加到 NSString(跟踪当前单词)
- 当用户按下空格(当前单词的结尾)时,对照词典检查字符串
- 如果匹配,则计算字符数并删除该字符数
- 输入词典建议的建议
- 清除字符串并重新开始
- 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
此外,您还可以使用 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 的个人体验