2017-11-10 6 views
0

UITextFiledと検索結果を提示したビューを含むページを作成し、検索のトリガ条件を変更したい。オリジナルのトリガ条件はtextFieldの入力テキストを変更していました。私はいくつかの変更を加えたいと思います。ユーザーがキーボードのオートコレクションに触れるときにのみ検索が開始されることを願っています。誰かが私に前進を与えてくれましたか、ありがとう! enter image description hereネイティブキーボードの自動入力文字列値を取得する方法

答えて

0

このようにUITextCheckerを実装してみてください。最後の2行は、2種類のprint文です。 1つは、スペルミスが発生したときに単語を推測するためのもので、もう1つは辞書単語の単語である。 arrPredectiveText配列には、辞書のように順番に単語が含まれます。あなたはこれから提案を得ることができます。

入力している文字列を使用してsuggestionsForCustomKeyboardメソッドを呼び出します。

-(void)suggestionsForCustomKeyboard:(NSString *)word{ 
    if ([currentString length] >= 1) { 

    UITextChecker *textChecker = [[UITextChecker alloc] init]; 
    NSRange misspelledRange = [textChecker 
           rangeOfMisspelledWordInString:currentString 
           range:NSMakeRange(0, [currentString length]) 
           startingAt:0 
           wrap:NO 
           language:@"en_US"]; 


    if (misspelledRange.location != NSNotFound) { 

     guesses = [textChecker guessesForWordRange:misspelledRange 
              inString:currentString 
              language:@"en_US"]; 
     NSLog(@"First guess: %@", [guesses firstObject]); 

    } else { 
     NSLog(@"Textchecker Not found"); 
     autocorrectedText = @""; 
    } 

    if (arrPredectiveText.count >= 2){ 
     suggestionOne = [arrPredectiveText objectAtIndex:0]; 
     suggestionTwo = [arrPredectiveText objectAtIndex:1]; 
    } 
    else if (arrPredectiveText.count == 1 && guesses.count >= 1) { 
     suggestionOne = [arrPredectiveText firstObject]; 
     suggestionTwo = [guesses firstObject]; 
    }else if (arrPredectiveText.count == 0 && guesses.count > 0){ 
     suggestionOne = [guesses firstObject]; 
     if (guesses.count > 1) { 
      suggestionTwo = [guesses objectAtIndex:1]; 
     } 
    } 

    NSLog(@"Textchecker all guess: %@", guesses); 
    NSLog(@"Prediction: %@",arrPredectiveText); 

} 
関連する問題