NSString *str = @"hipstar";
UITextChecker *textChecker = [[UITextChecker alloc] init];
NSRange misspelledRange = [textChecker rangeOfMisspelledWordInString:str range:NSMakeRange(0, [str length]) startingAt:0 wrap:NO language:@"en_US"];
NSArray *guesses = [NSArray array];
if (misspelledRange.location != NSNotFound) {
guesses = [textChecker guessesForWordRange:misspelledRange inString:str language:@"en_US"];
NSLog(@"First guess: %@", [guesses firstObject]);
// First guess: hipster
} else {
NSLog(@"Not found");
}
猜想结果
["hipster", "hip star", "hip-star", "hips tar", "hips-tar"]
学习功能:
// 学习一个新的单词
[UITextChecker learnWord:str];
NSRange misspelledRange = [textChecker rangeOfMisspelledWordInString:str range:NSMakeRange(0, [str length]) startingAt:0 wrap:NO language:@"en_US"];
结果呢?
NSString *partial = @"hipst";
NSArray *completions = [textChecker completionsForPartialWordRange:NSMakeRange(0, [partial length]) inString:partial language:@"en_US"];
// completions == ["hipster", "hipsters"]
哈哈~ 看到了给大家分享出来, 感觉以后有需求要用~ 毕竟是个跟强大的学习功能 ~
网友评论