How to use autocorrection and shortcut list in iOS8 custom keyboard?(如何在 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:
- 使用
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 的个人体验
这篇关于如何在 iOS8 自定义键盘中使用自动更正和快捷键列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 iOS8 自定义键盘中使用自动更正和快捷键
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01