Вы можете заставить UITextChecker работать точно, не добавляя новый словарь.
Я использую двухэтапный процесс, потому что мне нужно, чтобы первый шаг был быстрым (но не точным).Вам может понадобиться только второй шаг, который является точной проверкой.Обратите внимание, что для этого используется функцияручки UITextCheckercomptionsForPartialWordRange, поэтому она более точна, чем функция MisspelledWord.
// Шаг первый: я быстро проверяю, проходит ли проверка букв комбинация букв.Это не так точно, но очень быстро, поэтому я могу быстро исключить множество буквенных комбинаций (метод грубой силы).
UITextChecker *checker;
NSString *wordToCheck = @"whatever"; // The combination of letters you wish to check
// Set the range to the length of the word
NSRange range = NSMakeRange(0, wordToCheck.length - 1);
NSRange misspelledRange = [checker rangeOfMisspelledWordInString:wordToCheck range: range startingAt:0 wrap:NO language: @"en_US"];
BOOL isRealWord = misspelledRange.location == NSNotFound;
// Call step two, to confirm that this is a real word
if (isRealWord) {
isRealWord = [self isRealWordOK:wordToCheck];
}
return isRealWord; // if true then we found a real word, if not move to next combination of letters
// Шаг второй: дополнительная проверка, чтобы убедиться, что слово действительно является настоящим словом,возвращает true, если у нас есть настоящее слово.
-(BOOL)isRealWordOK:(NSString *)wordToCheck {
// we dont want to use any words that the lexicon has learned.
if ([UITextChecker hasLearnedWord:wordToCheck]) {
return NO;
}
// now we are going to use the word completion function to see if this word really exists, by removing the final letter and then asking auto complete to complete the word, then look through all the results and if its not found then its not a real word. Note the auto complete is very acurate unlike the spell checker.
NSRange range = NSMakeRange(0, wordToCheck.length - 1);
NSArray *guesses = [checker completionsForPartialWordRange:range inString:wordToCheck language:@"en_US"];
// confirm that the word is found in the auto-complete list
for (NSString *guess in guesses) {
if ([guess isEqualToString:wordToCheck]) {
// we found the word in the auto complete list so it's real :-)
return YES;
}
}
// if we get to here then it's not a real word :-(
NSLog(@"Word not found in second dictionary check:%@",wordToCheck);
return NO;
}