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.
Asked
Active
Viewed 721 times
4

Community
- 1
- 1

glenstorey
- 5,134
- 5
- 39
- 71
-
So I helped you out with this a few months ago, and now someone passing by posted a comment that I'm not sure I answered fully. Could you maybe help him out, down below my answer? (thanks!) – owlswipe Jun 27 '16 at 22:40
-
Hi @JohnRamos - no problem! – glenstorey Jun 28 '16 at 03:48
-
Thanks!! Much appreciated. – owlswipe Jun 28 '16 at 11:33
1 Answers
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 NSTextField
s to NSTextView
s. 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.
-
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
-
-
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
-
-
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
-
-
Don't call `becomeFirstResponder`. See [becomeFirstResponder](https://developer.apple.com/documentation/appkit/nsresponder/1526750-becomefirstresponder?language=occ). – Willeke Feb 08 '21 at 08:18