2014-09-26 164 views
7

如何在我自己的iOS8自定义键盘扩展中实现Apple的预测输入面板?如何使用自定义键盘扩展ios8的自动建议视图?

苹果专用键盘API文档Custom Keyboard状态:

RequestsOpenAccess设置BOOL是在info.plist中有通过UILexicon类访问 基本autocorrection词汇。使用此类的 以及您自己设计的词典,在用户输入文本时提供 suggestionsautocorrections

但我找不到如何在我的自定义键盘中使用UILexicon。我设置了RequestsOpenAccess设置YES

enter image description here

但仍然无法获得访问自定义词典像苹果的iOS8上的默认键盘字的建议做:

enter image description here

我的自定义键盘看起来像:

enter image description here

编辑:

我发现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]; 
     } 
    }]; 
} 

回答

8

芬利我做到了..!我把建议在使用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]; 
    } 

,我知道了我的键盘输出为:

enter image description here

+0

你是如何填充SUGGESTION表的?你从哪里得到所有的建议? – 2014-12-12 10:07:32

+0

我在我的回答中提到我使用静态sqlite数据库并使用LIKE查询,我从sqlite数据库中获得相关的字符建议工作。 @StanleyKubrick – 2014-12-12 11:45:55

+0

嘿Nitin哪里会是我们的数据库文件?在主应用程序或扩展? – jamil 2015-02-03 03:39:21

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']; 
} 
相关问题