11

I've seen that this question has been asked a few times, but no one seems to have an answer. I am trying to create an autocorrect feature on a custom keyboard, but I am completely lost as to how to do so. Apple gives some documentation, but it's not very detailed. I know it has something to do with UILexicon data, but I'm not sure what to do with it and how to use it to correct strings of text the user is typing.

Any help would be greatly appreciated.

What I have found so far:

let controller = UIInputViewController()

    controller.requestSupplementaryLexiconWithCompletion({
        lexicon in

        println(lexicon.description)

    })

But this is as far as I've gotten. Not sure what to do from here.

SomeGuy
  • 3,725
  • 3
  • 19
  • 23

1 Answers1

10

You are asking a very difficult question. In short, there is no built in access to autocorrect on iOS8; there is also no access to the APIs on iOS that allow the system to do things like show the red 'possible error' underline effect, or to other aspects of the system autocorrect behaviour, such as the in-place suggestion dialog (on iOS 7, or if you do not have the suggestions bar visible) or the 'will correct' blue background (on iOS 8).

things you can't do:

dotted red line

in-place error indicator

in-place suggestion

in-place autocorrect dialog

will correct

suggestions bar autocorrect indicator

what you can do is write your own autocorrect engine, from scratch, including both your own text processing and analysis and your own user-interface idioms. This has to all be done with the limitation that you are not able to draw outside of your keyboard's bounds, and you cannot modify anything else on screen. A number of third-party keyboards do this, such as minuum and swiftkey (disclaimer: I work on minuum) but it is a non-incidental amount of work. If you're interested in playing around with this, a good place to start might be the built in UITextChecker class, although auto-correction is ultimately a problem distinct from spell-checking.

UILexicon is only useful once you've already implemented things; all that it really offers you is a list of words that you can use to supplement whatever dictionary you're using, as well as to implement any text shortcuts your user might have added in their system settings. It is not, on its own, enough to build an auto-correction system from.

addendum: How to Write a Spelling Corrector is a great little essay / tutorial by Peter Norvig that you might find interesting, and that I would recommend even if you're not trying to write auto-correct.

cmyr
  • 2,235
  • 21
  • 30
  • 2
    When I initially asked this question, I checked back every day for an answer and eventually gave up. I ended up creating a very primitive autocorrect by typing on my keyboard without looking at it, and then adding all the misspelled words as a key on a Plist with the correct word as it's value. Then each word typed, it checks to see if it has a pair. It's not efficient at all and it takes forever to add new words, but it works! The article you added is EXACTLY what I've been looking for for forever. So thank you so much for your answer. My keyboard is a Dvorak layout if you care by the way! – SomeGuy Apr 27 '15 at 11:07
  • I am stuck trying to change the language or locale of my keyboard extension. If I run my extension in English and tries to edit a Swedish text document in Pages, all words are marked with red. If I switch to SwiftKey, I can toggle between English and Swedish and the spell check result in Pages will update according to the selected language. I can't find a way to do this with the public api:s though. – Daniel Saidi Mar 25 '21 at 13:39