如何在我自己的iOS8自定义键盘扩展程序中实现Apple的预测输入面板?
Apple的自定义键盘API文档Custom Keyboard声明:
RequestsOpenAccess
设置BOOL
是的,在info.plist中可以访问a 通过UILexicon类的基本autocorrection
词典。利用 本课程,以及您自己设计的词典,提供 用户输入文字时suggestions
和autocorrections
。
但是我找不到如何在我的自定义键盘中使用UILexicon
。我将RequestsOpenAccess
设置为YES
:
但仍无法访问自定义字典,例如Apple的iOS8默认键盘这样的字词建议:
我的自定义键盘如下:
修改
我发现用requestSupplementaryLexiconWithCompletion
这样的UILexicon class
我尝试使用以下代码实现此目的:
- (void)viewDidLoad {
[super viewDidLoad];
[self requestSupplementaryLexiconWithCompletion:^(UILexicon *appleLex) {
appleLexicon = appleLex;
NSUInteger lexEntryCount = appleLexicon.entries.count;
for(UILexiconEntry *entry in appleLexicon.entries) {
NSString *userInput = [entry userInput];
NSString *documentText = [entry documentText];
lable.text=userInput;
[lable setNeedsDisplay];
}
}];
}
答案 0 :(得分:8)
Finlay我做到了..!我使用sqlite静态数据库提出建议并使用类似查询获得前三个建议的工作,如下面的代码:
NSString *precedingContext = self.textDocumentProxy.documentContextBeforeInput; //here i get enter word string.
__block NSString *lastWord = nil;
[precedingContext enumerateSubstringsInRange:NSMakeRange(0, [precedingContext length]) options:NSStringEnumerationByWords | NSStringEnumerationReverse usingBlock:^(NSString *substring, NSRange subrange, NSRange enclosingRange, BOOL *stop) {
lastWord = substring;
*stop = YES;
}];
NSLog(@"==%@",lastWord); // here i get last word from full of enterd string
NSString *str_query = [NSString stringWithFormat:@"select * from suggestion where value LIKE '%@%%' limit 3",lastWord];
NSMutableArray *suggestion = [[DataManager initDB] RETRIVE_Playlist:str_query];
NSLog(@"arry %@",suggestion); i get value in to array using like query
if(suggestion.count>0)
{
if(suggestion.count==1)
{
[self.ObjKeyLayout.FirstButton setTitle:[suggestion objectAtIndex:0] forState:UIControlStateNormal];
}
else if(suggestion.count==2)
{
[self.ObjKeyLayout.FirstButton setTitle:[suggestion objectAtIndex:0] forState:UIControlStateNormal];
[self.ObjKeyLayout.secondButton setTitle:[suggestion objectAtIndex:1] forState:UIControlStateNormal];
}
else
{
[self.ObjKeyLayout.FirstButton setTitle:[suggestion objectAtIndex:0] forState:UIControlStateNormal];
[self.ObjKeyLayout.secondButton setTitle:[suggestion objectAtIndex:1] forState:UIControlStateNormal];
[self.ObjKeyLayout.thirdButton setTitle:[suggestion objectAtIndex:2] forState:UIControlStateNormal];
}
}
else
{
[self.ObjKeyLayout.FirstButton setTitle:@"" forState:UIControlStateNormal];
[self.ObjKeyLayout.secondButton setTitle:@"" forState:UIControlStateNormal];
[self.ObjKeyLayout.thirdButton setTitle:@"" forState:UIControlStateNormal];
}
我知道我的键盘输出是:
答案 1 :(得分:0)
这个答案可能对某人有帮助。
+ (void)getSuggestionsFor:(NSString *)word WithCompletion:(void(^)(NSArray *))completion
{
NSString *prefix = [word substringToIndex:word.length - 1];
// Won't get suggestions for correct words, so we are scrambling the words
NSString *scrambledWord = [NSString stringWithFormat:@"%@%@",word, [self getRandomCharAsNSString]];
UITextChecker *checker = [[UITextChecker alloc] init];
NSRange checkRange = NSMakeRange(0, scrambledWord.length);
NSRange misspelledRange = [checker rangeOfMisspelledWordInString:scrambledWord range:checkRange startingAt:checkRange.location wrap:YES language:@"en_US"];
NSArray *arrGuessed = [checker guessesForWordRange:misspelledRange inString:scrambledWord language:@"en_US"];
// NSLog(@"Arr ===== %@",arrGuessed);
// Filter the result based on the word
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[c] %@",word];
NSArray *arrayfiltered = [arrGuessed filteredArrayUsingPredicate:predicate];
if(arrayfiltered.count == 0)
{
// Filter the result based on the prefix
NSPredicate *newPredicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[c] %@",prefix];
arrayfiltered = [arrGuessed filteredArrayUsingPredicate:newPredicate];
}
completion(arrayfiltered);
}
+ (NSString *)getRandomCharAsNSString {
return [NSString stringWithFormat:@"%c", arc4random_uniform(26) + 'a'];
}