이와 같이 UITextChecker를 구현해보십시오. 마지막 두 줄은 두 가지 유형의 print 문입니다. 하나는 철자가 잘못되었을 때 단어를 추측하고 다른 하나는 사전 단어를 나타내는 단어입니다. 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);
}