更新时间:2023-02-03 09:21:08
实现词典看起来非常像这样:
Implementing the lexicon would look pretty much like this:
requestSupplementaryLexiconWithCompletion()
在启动时获取词典。 NSString
(跟踪当前单词)requestSupplementaryLexiconWithCompletion()
to get the lexicon upon launch once.NSString
(tracking the current word)此外,您还可以使用 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