Puede hacer que UITextChecker funcione correctamente sin necesidad de agregar un nuevo diccionario.
Uso un proceso de dos pasos porque necesito que el primer paso sea rápido (pero no preciso). Es posible que solo necesite el paso dos, que es el control preciso. Tenga en cuenta que esto hace uso de la función CompletionsForPartialWordRange de UITextChecker, razón por la cual es más precisa que la función MisspelledWord.
// Paso uno: Compruebo rápidamente para ver si una combinación de letras pasa el corrector ortográfico. Esto no es tan preciso, pero es muy rápido, por lo que puedo excluir rápidamente muchas combinaciones de letras (enfoque de fuerza bruta).
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
// Paso dos: comprobación adicional para asegurarse de que la palabra es realmente una palabra real. devuelve verdadero si tenemos una palabra real.
-(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;
}
¿Por qué no usar n NSSpellChecker? – Goz
@Wolfgang: como NSObject NSString .... – brain
@Wolfgang: NS clases con prefijo están disponibles en iOS, como NSObject, NSNumber, NSString, NSPredicate, NSFetchedController, ... – rckoenes