有没有办法使用UITextChecker不区分大小写?

时间:2014-05-02 18:37:33

标签: ios nsstring

我需要我的应用来检查用户输入的单词是否真的是真实的单词。为此,我找到了UITextChecker。我用它来检查这样的字:

UITextChecker *textChecker = [[UITextChecker alloc] init];
NSLocale *locale = [NSLocale currentLocale];
NSString *language = [locale objectForKey:NSLocaleLanguageCode];
NSRange searchRange = NSMakeRange(0, [currentWord length]);

NSRange misspelledRange = [textChecker rangeOfMisspelledWordInString:currentWord range: searchRange startingAt:0 wrap:NO language:language];
if (misspelledRange.location == NSNotFound) NSLog(@"is a word");
else NSLog(@"is not a word");

这样可以正常工作,直到以大写字母开头的单词(德语中有很多单词:))。用户输入文本小写,因此即使单词实际上是单词,它也会输出“不是单词”,因为它是小写的。 我在文档中找不到解决此问题的方法,我也在这里搜索了它。 所以我的问题是,如果检查失败,是否有更好的方法可以将第一个字母转换为大写?

如果没有别的办法我会做什么的例子:

NSRange misspelledRange = [textChecker rangeOfMisspelledWordInString:currentWord range: searchRange startingAt:0 wrap:NO language:language];
if (misspelledRange.location == NSNotFound) NSLog(@"is a word");
else {
    NSString *firstLetter = [currentWord substringToIndex:1];
    NSString *restWord = [currentWord substringFromIndex:1];

    currentWord = [NSString stringWithFormat:@"%@%@", [firstLetter capitalizedString], restWord];

    NSRange misspelledRange = [textChecker rangeOfMisspelledWordInString:currentWord range: searchRange startingAt:0 wrap:NO language:language];
    if (misspelledRange.location == NSNotFound) NSLog(@"is a word");
    NSLog(@"is not a word");
}

感谢您的帮助!

0 个答案:

没有答案
相关问题