美文网首页
UIText​Checker

UIText​Checker

作者: Stark_Dylan | 来源:发表于2016-05-15 13:00 被阅读378次
    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"]
    

    原文地址

    哈哈~ 看到了给大家分享出来, 感觉以后有需求要用~ 毕竟是个跟强大的学习功能 ~

    相关文章

      网友评论

          本文标题:UIText​Checker

          本文链接:https://www.haomeiwen.com/subject/ciyvrttx.html