PATH  Documentation > Mac OS X > Application Kit Reference: Java

Table of Contents

NSSpellChecker


Inherits from:
NSObject
Package:
com.apple.yellow.application


Class Description


The NSSpellChecker class gives any application an interface to the Cocoa spell-checking service. To handle all its spell checking, an application needs only one instance of NSSpellChecker. It provides a panel in which the user can specify decisions about words that are suspect. To check the spelling of a piece of text, the application:

The checkSpellingOfString method checks the spelling of the words in the specified string beginning at the specified offset (this example uses 0 to start at the beginning of the string) until it finds a word that is misspelled. Then it returns an NSRange to indicate the location of the misspelled word.

In a graphical application, whenever a misspelled word is found, you'll probably want to highlight the word in the document, using the NSRange that checkSpellingOfString returned to determine the text to highlight. Then you should show the misspelled word in the Spelling panel's misspelled-word field by calling updateSpellingPanelWithMisspelledWord. If checkSpellingOfString does not find a misspelled word, you should call updateSpellingPanelWithMisspelledWord with the empty string. This causes the system to beep, letting the user know that the spell check is complete and no misspelled words were found. None of these steps is required, but if you do one, you should do them all.

The object that provides the string being checked should adopt the following protocols:


Protocol Description
NSChangeSpelling A message in this protocol (changeSpelling:) is sent down the responder chain when the user presses the Correct button.
NSIgnoreMisspelledWords When the object being checked responds to this protocol, the spell server keeps a list of words that are acceptable in the document and enables the Ignore button in the Spelling panel.

The application may choose to split a document's text into segments and check them separately. This will be necessary when the text has segments in different languages. Spell checking is invoked for one language at a time, so a document that contains portions in three languages will require at least three checks.


Dictionaries and Word Lists


The process of checking spelling makes use of three references:

A word is considered to be misspelled if none of these three accepts it.


Matching a List of Ignored Words with the Document It Belongs To


The String being checked isn't the same as the document. In the course of processing a document, an application might run several checks based on different parts or different versions of the text. But they'd all belong to the same document. The NSSpellChecker keeps a separate "ignored words" list for each document that it checks. To help match "ignored words" lists to documents, you should call uniqueSpellDocumentTag once for each document. This method returns a unique arbitrary integer that will serve to distinguish one document from the others being checked and to match each "ignored words" list to a document. (The convenience method checkSpellingOfString takes no tag. This method is suitable when the first responder does not conform to the NSIgnoreMisspelledWords protocol.)

When the application saves a document, it may choose to retrieve the "ignored words" list and save it along with the document. To get back the right list, it must send the NSSpellChecker an ignoredWords message. When the application has closed a document, it should notify the NSSpellChecker that the document's "ignored words" list can now be discarded, by sending it a closeSpellDocumentWithTag message. When the application reopens the document, it should restore the "ignored words" list with the message setIgnoredWords.




Method Types


Constructors
NSSpellChecker
Getting the spell checker
sharedSpellChecker
sharedSpellCheckerExists
Managing the spelling panel
setAccessoryView
accessoryView
spellingPanel
Checking spelling
countWordsInString
checkSpellingOfString
Setting the language
setLanguage
language
Managing the Spelling Process
uniqueSpellDocumentTag
closeSpellDocumentWithTag
ignoreWord
setIgnoredWords
ignoredWords
setWordFieldStringValue
updateSpellingPanelWithMisspelledWord


Constructors



NSSpellChecker

public NSSpellChecker()

Description forthcoming.


Static Methods



sharedSpellChecker

public static NSSpellChecker sharedSpellChecker()

Returns the NSSpellChecker (one per application).

See Also: sharedSpellCheckerExists



sharedSpellCheckerExists

public static boolean sharedSpellCheckerExists()

Returns whether the application's NSSpellChecker has already been created.

See Also: sharedSpellChecker



uniqueSpellDocumentTag

public static int uniqueSpellDocumentTag()

Returns a guaranteed unique tag to use as the spell-document tag for a document. Use this method to generate tags to avoid collisions with other objects that can be spell-checked.


Instance Methods



accessoryView

public NSView accessoryView()

Returns the Spelling panel's accessory NSView object.

See Also: setAccessoryView



checkSpellingOfString

public NSRange checkSpellingOfString( String stringToCheck, int startingOffset)

Starts the search for a misspelled word in stringToCheck starting at startingOffset within the string object. Returns the range of the first misspelled word. Wrapping occurs but no ignored-words dictionary is used.

closeSpellDocumentWithTag

public void closeSpellDocumentWithTag(int tag)

Notifies the spell checker that the user has finished with the ignored-word document identified by tag, causing it to throw that dictionary away.

countWordsInString

public int countWordsInString( String stringToCount, String language)

Returns the number of words in stringToCount. The language argument specifies the language used in the string. If language is the empty string, the current selection in the Spelling panel's pop-up menu is used.

ignoreWord

public void ignoreWord( String wordToIgnore, int tag)

Instructs the spell checker to ignore all future occurrences of wordToIgnore in the document identified by tag. You should invoke this method from within your implementation of the NSIgnoreMisspelledWords protocol's ignoreSpelling method.

ignoredWords

public NSArray ignoredWords(int tag)

Returns the array of ignored words for a document identified by tag. Invoke this before closeSpellDocumentWithTag if you want to store the ignored words.

See Also: setIgnoredWords



language

public String language()

Returns the current language used in spell-checking.

See Also: setLanguage



setAccessoryView

public void setAccessoryView(NSView aView)

Makes an NSView object an accessory of the Spelling panel by making it a subview of the panel's content view. This method posts a WindowDidResizeNotification with the Spelling panel object to the default notification center.

See Also: accessoryView



setIgnoredWords

public void setIgnoredWords( NSArray someWords, int tag)

Initializes the ignored-words document (a dictionary identified by tag with someWords), an array of words to ignore.

See Also: ignoredWords



setLanguage

public boolean setLanguage(String language)

Sets the language to use in spell-checking to language. Returns whether the Language pop-up list in the Spelling panel lists language.

See Also: language



setWordFieldStringValue

public void setWordFieldStringValue(String aString)

Sets the string that appears in the misspelled word field, using the string object aString.

spellingPanel

public NSPanel spellingPanel()

Returns the spell checker's panel.

updateSpellingPanelWithMisspelledWord

public void updateSpellingPanelWithMisspelledWord(String word)

Causes the spell checker to update the Spelling panel's misspelled-word field to reflect word. You are responsible for highlighting word in the document and for extracting it from the document using the range returned by the checkSpelling:... methods. Pass the empty string as word to have the system beep, indicating no misspelled words were found.


Table of Contents