Możesz wykonać UITextChecker działa dokładnie bez konieczności dodawania nowego słownika.
Używam procesu dwuetapowego, ponieważ potrzebuję pierwszego kroku, aby być szybkim (ale nie dokładnym). Możesz potrzebować tylko drugiego kroku, który jest dokładnym sprawdzeniem. Zauważ, że korzysta to z funkcji uzupełnień UFTextCheckerForPartialWordRange, dlatego jest bardziej dokładna niż funkcja MisspelledWord.
// Krok pierwszy: Szybko sprawdzam, czy kombinacja liter przechodzi sprawdzanie pisowni. Nie jest to dokładne, ale jest bardzo szybkie, więc mogę szybko wykluczyć wiele kombinacji liter (podejście "brute force").
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
// Krok drugi: Dodatkowa kontrola, aby upewnić się, że słowo jest naprawdę prawdziwe. zwraca prawdę, jeśli mamy prawdziwe słowo.
-(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;
}
Dlaczego po prostu nie używać n NSSpellChecker? – Goz
@Wolfgang: podobnie jak NSObject NSString .... – brain
@Wolfgang: klasy z prefiksem NS są dostępne na iOS, takie jak NSObject, NSNumber, NSString, NSPredicate, NSFetchedController, ... – rckoenes