简体   繁体   中英

UITextChecker completionsForPartialWordRange

I'm building custom keyboard extension and want to implement autocompletion, like Apple does. As I see method completionsForPartialWordRange returns list of words sorted alphabetically. How can I get results sorted by usage?

在此处输入图片说明

The docs for completionsForPartialWordRange:inString:language: say:

The strings in the array are in the order they should be presented to the user—that is, more probable completions come first in the array.

However, the results are very clearly sorted in alphabetical order, and it's not true that "more probable completions come first in the array." The below was tested with iOS 9:

NSString *partialWord = @"th";
UITextChecker *textChecker = [[UITextChecker alloc] init];
NSArray *completions = [textChecker completionsForPartialWordRange:NSMakeRange(0, partialWord.length) inString:partialWord language:@"en"];

iOS word completions for "th" :

thalami,
thalamic,
thalamus,
thalassic,
thalidomide,
thallium,
...
the,
...

So, the results will need to be sorted again after obtaining the word completions.

The OS X NSSpellChecker version of this method does not have the same problem:

NSString *partialWord = @"th";

NSArray *completions = [[NSSpellChecker sharedSpellChecker] completionsForPartialWordRange:NSMakeRange(0, partialWord.length) inString:partialWord language:@"en" inSpellDocumentWithTag:0];

List of complete words from the spell checker dictionary in the order they should be presented to the user.

Mac OS X word completions for "th" :

the,
this,
that,
they,
thanks,
there,
that's,
...

Filing a radar bug report would be a good idea, so that the behavior will hopefully be fixed in a later version of iOS. I've reported this as rdar://24226582 if you'd like to duplicate.

Swift 4.0

 func autoSuggest(_ word: String) -> [String]? {
    let textChecker = UITextChecker()
    let availableLangueages = UITextChecker.availableLanguages
    let preferredLanguage = (availableLangueages.count > 0 ? availableLangueages[0] : "en-US");

    let completions = textChecker.completions(forPartialWordRange: NSRange(0..<word.utf8.count), in: word, language: preferredLanguage)

    return completions
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM