MatTheWhale MatTheWhale - 1 year ago 75
C# Question

NSTextView auto spell checking not working porperly

I have a class that inherits from NSTextView, which, when initialized, sets up auto spell checking with

and when focused, sets itself as the first responder. Based on all the questions and answers I've seen on SO so far, it seems this should be enough to ensure the spell checker works properly.

But when I type gibberish into the view, it won't underline any misspelled words unless the word was typed relatively quickly. That is, when I type "asdf" with a space after it in under about a half of a second, the spellchecker underlines it properly. But if I type "asdf" any slower, or even if I rapidly type "asdf" but wait a second before adding a space, no underline will be shown on the word.

This leads to the possibility of the same word typed multiple times having conflicting spelling evaluations, shown below.

There was even one time during testing where I saw a red underline appear for a split second, and then immediately disappear from a misspelled word I had typed.

Also worth noting is that if the focus leaves and reenters the text view, after about 1 second after focus all the squiggly underlines appear in the correct spots under all the misspelled words. Proceeding to type additional text, however, still raises the problem I mentioned above.

This means that misspelled words will not be underlined unless

  1. They are typed unreasonably quickly

  2. They had been typed into the text view prior to focusing the view

As I said before I have looked at all relevant SO threads, and I have also played around with the calls to
extensively, and I've just about run out of ideas.

Quick update, since last week I've tried a few more things, one of which shows promise. Using the TextView function
, I am able to programmatically force the red squiggly underline to appear anywhere within the TextView that I please. Although this seems promising, using this function has instead uncovered another buggy behavior: When I call this function and make a red squiggly appear somewhere in the view, the squiggly line will only appear briefly. The squiggly red underline appears, and after roughly the same amount of delay as I saw inthe "asdf" bug, the squiggly line disappears. This leads me to think that the problem might be caused in the C# code that overrides some of the Cocoa API.

Is this an OSX issue? Why is something as arbitrary as the speed at which I type the determining factor for whether the spell checker will correctly underline a word?

P.S. The code is in a syntax different from Objective-C syntax because I am using MCocoa, which is a tool that machine-generated wrappers for the Cocoa API. The C# code that I am currently using to reference NSTextView was generated from the MacOSX 10.5 SDK

Answer Source

When using setSpellingState, the squiggly line would sometimes appear and immediately disappear because of a race condition between setSpellingState and the auto spell-checker. It seems like if they both tried to highlight a word at the same time, they would cancel each other out. Since the auto spell checker's behavior is what's causing this whole problem in the first place, I just replaced setContinuousSpellCheckingEnabled(true) with setContinuousSpellCheckingEnabled(false), and now the red squiggle underline appears correctly, no matter how I type the word! As long as it's misspelled of course :)

For anyone having issues with NSTextView's auto spell checker, I was able to simply disable the auto spell checker using setContinuousSpellCheckingEnabled(false), and hacking in my own automatic spell checker using NSSpellChecker.shared() to check for misspelled words, and setSpellingState to force-draw the red squiggly spelling indicator under the misspelled words founds by the NSSpellChecker. All this was done within the implementation of textViewDidChangeSelection, so that words could be checked every time the selection was changed by either typing, moving the cursor from an incomplete word, or pasting text.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download