Autocorrection and Suggestion for custom keyboard

2.1k views Asked by At

I want to add Autocorrection and Suggestion to my custom keyboard.

There are already several similar questions on Stack, but there is only said about UILexicon which as I understood is only used to get user's shortcuts and that there is no way to acces Apple Autocorrection and Suggestion library.

I also saw some questions regarding UITextChecker but don't know if it has an access to Apple's native sugesstion library?

May be there are some new classes for that?

1

There are 1 answers

5
DarkAgeOutlaw On BEST ANSWER

I use four different systems for my keyboard:

  1. I have a list of the top 30,000 or so words, ranked in order of the most used to the least used. You can pay for lists, I just got a free one of about 42,000 and edited it down a lot.

  2. guessesForWordRange is provided by Apple. It will guess words that are close to what you have typed. It does a fairly good job, but I had to filter out some things. The top guess sometimes has quotation marks around it, but other then that it works great.

  3. completionsForPartialWordRange is also provided by Apple. It will return completed words, but in alphabetical order, not ranked by usage. Not much good on it's own, but is a great supplement to 1 and 2. (if this worked correctly #1 wouldn't be needed)

  4. Special cases. Mainly for contractions. When someone types didnt, I wanted it to auto choose didn't. So I have almost all contractions specifically programmed in.

So my word suggestions and autocorrection aren't perfect, but it does a decent job.

Hope this helps.

Edit: As of iOS 16 it seems completionsForPartialWordRange is working correctly, so having your own list of words shouldn't be needed anymore.