3

Question

In some applications, such as mail clients or Twitter clients, I find myself typing away at something, everything looks good and right when I press the send/Tweet button the text view automatically corrects the last word to an incorrect spelling. Obviously at that point I waited just the wrong amount of time after finishing typing before sending it so the spell checking was still going on.

I guess the first question here really should be what do you think about removing that functionality? Because on the other hand I'm sure that exact same thing happens to people but it actually fixes the spelling of the last word as opposed to messing it up. Otherwise if you think this is a valid idea is there a way to disable automatic spelling correction when a NSTextView loses focus?

What I've looked at:

What I actually tried (in Xcode in an empty project)

  • Implementing the NSTextDelegate textShouldBeginEditing: and textShouldEndEditing: and inside of calling [self.textView setAutomaticSpellingCorrectionEnabled:true]; and [self.textView setAutomaticSpellingCorrectionEnabled:false]; respectively (at first I also called NSTextView's setAutomaticTextReplacementEnabled: but that's just for user settings like (c) to the copyright symbol)

  • In the same textShouldBeginEditing: and textShouldEndEditing: (from above) setting the NSTextView's enabledTextCheckingTypes to NSTextCheckingAllTypes and NSTextCheckingAllTypes - NSTextCheckingTypeCorrection respectively.

  • Subclassing NSTextView and implementing becomeFirstResponder and resignFirstResponder and in them changing the same properties as above.

  • Calling NSSpellChecker methods from either resignFirstResponder or textShouldEndEditing: (this works with [[NSSpellChecker sharedSpellChecker] dismissCorrectionIndicatorForView:self];) to hide the popup but it still corrects the spelling)

Example

I've noticed this functionality in Tweetbot you can test it using foriegn vs foreign. If you type it in and Tweet it while the bubble is still up it will Tweet the incorrect spelling.

Community
  • 1
  • 1
Keith Smiley
  • 61,481
  • 12
  • 97
  • 110
  • Would love to hear about why this was worth a downvote? – Keith Smiley Feb 14 '13 at 14:51
  • Another question you might want to take a look at - http://stackoverflow.com/questions/1985357/how-to-disable-autocomplete-in-uitextview-iphone-keyboard – HaemEternal Jan 13 '15 at 13:20
  • I don’t have an answer to the question on how to achieve the change, but I would argue that it’s **not** a good idea to change the behaviour at all. The reason is that in general people prefer to have consistent behaviour between apps for similar controls, even if it can be slightly annoying. So, unless you are sure that every user will run into the situation where they will be annoyed, don’t break consistency. My two cents. – alloy Feb 14 '13 at 14:57
  • Thanks but it looks like that's only for iOS – Keith Smiley Jan 13 '15 at 16:28

1 Answers1

1

The solution is to subclass NSTextView and override the handleTextCheckingResults: method.

- (void)handleTextCheckingResults:(NSArray<NSTextCheckingResult *> *)results forRange:(NSRange)range types:(NSTextCheckingTypes)checkingTypes options:(NSDictionary<NSTextCheckingOptionKey,id> *)options orthography:(NSOrthography *)orthography wordCount:(NSInteger)wordCount {
    for (NSTextCheckingResult *result in results) {
        if (result.resultType == NSTextCheckingTypeSpelling) {
            // you can either suppress all corrections by using `return` here

            // or you can compare the original string to the replacement like this:
            NSString *originalString = [self.string substringWithRange:result.range];
            NSString *replacement = result.replacementString;

            // or you can do something more complex (like suppressing correction only under
            // certain conditions

            return; // we don't do the correction
        }
    }

    // default behaviors, including auto-correct
    [super handleTextCheckingResults:results forRange:range types:checkingTypes options:options orthography:orthography wordCount:wordCount];
}

This works for all of the NSTextCheckingTypes.

Nima Yousefi
  • 817
  • 6
  • 11