4

How can I check the spelling of a NSTextField using swift? I'm already using controlTextDidChange to validate the text. This solution seems to mention casting the first responder as a NSTextView but I'm not sure that is possible with swift using coercion. I know this would be easier if I changed to a NSTextView but if possible I'd like to avoid this.

Community
  • 1
  • 1
glenstorey
  • 5,134
  • 5
  • 39
  • 71

1 Answers1

2

This should help you out.

// Focus TextField
phraseTextField.becomeFirstResponder()
// Enable Continous Spelling
let textView: NSTextView = (self.window!.firstResponder as! NSTextView)
textView.continuousSpellCheckingEnabled = true

Adapted from: How do I enable spell checking within an NSTextField on Mac OS X?


In other situations, it may just work better to change NSTextFields to NSTextViews. Simply use a "Text View" (search NSTextView in the Object Library) and spell check will be on by default. NSTextFields simply do not support spell check in some cases, as best I can tell.

Also consult: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSTextView_Class/#//apple_ref/doc/uid/20000373-SW55

Community
  • 1
  • 1
owlswipe
  • 19,159
  • 9
  • 37
  • 82
  • I think this method would work in most cases, but to add complexity to the situation it's a NSStatusBar app - with no menu. So in my situation the above code just hangs the app. – glenstorey Feb 29 '16 at 16:26
  • @glenstorey I amended my answer, let me know if it helps!! – owlswipe Mar 01 '16 at 00:50
  • Hey - so I came back to this problem after googling it, turns out your solution works really well (just not when you do the .becomeFirstResponder in viewDidLoad for now, obvious reasons). Thanks so much! – glenstorey Jun 02 '16 at 18:07
  • @glenstorey glad I could help! – owlswipe Jun 02 '16 at 20:49
  • Where do I need to put this code? I'm pretty new to swift and trying to get this to work since a NSTextView wont allow tabbing to the next field. I tried in the ViewController.swift viewDidLoad then saw the comment above. The error I get is self doesn't have a window property. Sorry for the bad question. – Adam Jun 27 '16 at 20:51
  • @Adam So I'm more of an iOS guy, but my guess would be that this goes in a delegate function that gets called when a text field becomes the first responder. In iOS Swift this would be `func textFieldDidBeginEditing(textField: UITextField)`. This article might help for OSX: http://stackoverflow.com/questions/28807742/nstextfield-gotfocus-event-cocoa-swift Also, let me consult with `glenstorey`, he'll know :D. – owlswipe Jun 27 '16 at 22:39
  • Hi @Adam - if the only reason you're not using `NSTextView` is that it doesn't allow tabbing, read this question http://stackoverflow.com/a/2485987/459116 - it shows how to support tabbing, and is less of a workaround than getting `NSTextField` to check spelling. If you need a bit more detail on getting spelling working, comment back and I'll add more details. – glenstorey Jun 28 '16 at 03:47
  • Well it also seems that NSTextField is easier to limit character count with, though I only looked at that stuff briefly. Fortunately this is a dev tool I'm working on and I can just tell people to use the "edit>spelling and grammar>check spelling while typing" for now. Thank you both for being so helpful. – Adam Jun 29 '16 at 00:20
  • @Adam Ok. Let me know if there is anything else I can do to help. – owlswipe Jun 29 '16 at 01:16
  • Don't call `becomeFirstResponder`. See [becomeFirstResponder](https://developer.apple.com/documentation/appkit/nsresponder/1526750-becomefirstresponder?language=occ). – Willeke Feb 08 '21 at 08:18