3

Is there some notification that is posted or some other way to tell when in a NSTextView or any editable element that something has been autocorrected?

Joshua
  • 15,200
  • 21
  • 100
  • 172

2 Answers2

4

I have in fact found out how to do this using the NSTextView spell checker delegate methods:

- (NSArray *)textView:(NSTextView *)view didCheckTextInRange:(NSRange)range types:(NSTextCheckingTypes)checkingTypes options:(NSDictionary *)options results:(NSArray *)results orthography:(NSOrthography *)orthography wordCount:(NSInteger)wordCount {
    for (NSTextCheckingResult *result in results) {
        if (result.resultType == NSTextCheckingTypeCorrection) {
            NSLog(@"autocomplete has occured! %@", result);
        }
    }
    return results;
}
Joshua
  • 15,200
  • 21
  • 100
  • 172
2

Take a look at NSSpellCheckerDidChangeAutomaticSpellingCorrectionNotification.

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
  • That sounds like what I need but when I subscribe to it, it never get's called when auto-correct takes place. – Joshua Aug 07 '11 at 07:51
  • 1
    Perhaps a bug? Perhaps post some relevant code for others to review. – Alex Reynolds Aug 07 '11 at 08:18
  • I've been subscribing to the notification like so: '[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(autocompleteOccurred:) name: NSSpellCheckerDidChangeAutomaticSpellingCorrectionNotification object:nil]' where the 'autocompleteOccured' method simply contains an NSLog. Then using the NSTextView in my app I cause a autocorrect but I never see the message logged. – Joshua Aug 07 '11 at 10:44
  • 3
    The `NSSpellChecker` notifications are only posted when the user toggles the setting in System Preferences to enable/disable automatic spelling correction/text replacement. – 一二三 Aug 07 '11 at 12:25
  • 1
    Really? It doesn't sound like that in the description for the notification. – Joshua Aug 07 '11 at 13:17
  • No, you're right the notification is in fact called when the options is changed in System Preferences. Ah, shame I thought this sounded like exactly what I wanted. – Joshua Aug 07 '11 at 15:43
  • Perhaps you could warn your user to enable that setting on first launch, or run an AppleScript to do it. Other apps do this sort of thing, too. It might not be the end of the world. – Alex Reynolds Aug 08 '11 at 01:22
  • You might also file a feature enhancement request at http://bugreporter.apple.com If you file a radar number here, maybe others can duplicate it if they come across this question in the future. – Alex Reynolds Aug 08 '11 at 01:23
  • I don't need to worry about the setting, I just need to know when in the text view in my app an auto-completion has taken place. Seeing as though I can find a way to do this I will take up your suggestion and file a feature request. – Joshua Aug 08 '11 at 06:38