7

如何在我自己的 iOS8 自定义键盘扩展中实现 Apple 的预测输入面板?

Apple 的自定义键盘 API 文档自定义键盘 状态:

RequestsOpenAccess在 info.plist 中设置yes 可以通过 UILexicon 类BOOL访问基本词典。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];
        }
    }];
}
4

2 回答 2

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];
    }

我明白了,我的键盘输出是:

在此处输入图像描述

于 2014-10-10T13:30:20.140 回答
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'];
}
于 2015-12-09T11:22:16.670 回答