| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/AppKit.framework |
| Availability | Available in Mac OS X v10.0 and later. |
| Companion guide | |
| Declared in | NSSpellChecker.h |
| Related sample code |
The NSSpellChecker class provides an interface to the Cocoa spell-checking service. To handle all its spell checking, an application needs only one instance of NSSpellChecker, known as the spell checker. Through the spell checker you manage the Spelling panel, in which the user can specify decisions about words that are suspect.
The spell checker also offers the ability to provide word completions to augment the text completion system in Mac OS X v10.3.
– availableLanguages
– userPreferredLanguages
– automaticallyIdentifiesLanguages
– setAutomaticallyIdentifiesLanguages:
– language
– setLanguage:
– spellingPanel
– substitutionsPanel
– updateSpellingPanelWithGrammarString:detail:
– updatePanels
– accessoryView
– setAccessoryView:
– substitutionsPanelAccessoryViewController
– setSubstitutionsPanelAccessoryViewController:
– countWordsInString:language:
– checkSpellingOfString:startingAt:
– checkSpellingOfString:startingAt:language:wrap:inSpellDocumentWithTag:wordCount:
– checkGrammarOfString:startingAt:language:wrap:inSpellDocumentWithTag:details:
– checkString:range:types:options:inSpellDocumentWithTag:orthography:wordCount:
– requestCheckingOfString:range:types:options:inSpellDocumentWithTag:completionHandler:
– guessesForWordRange:inString:language:inSpellDocumentWithTag:
– guessesForWord: Deprecated in Mac OS X v10.6
+ uniqueSpellDocumentTag
– closeSpellDocumentWithTag:
– ignoreWord:inSpellDocumentWithTag:
– ignoredWordsInSpellDocumentWithTag:
– setIgnoredWords:inSpellDocumentWithTag:
– setWordFieldStringValue:
– updateSpellingPanelWithMisspelledWord:
– completionsForPartialWordRange:inString:language:inSpellDocumentWithTag:
– hasLearnedWord:
– unlearnWord:
– learnWord:
– userQuotesArrayForLanguage:
– userReplacementsDictionary
– forgetWord: Deprecated in Mac OS X v10.5
Returns the NSSpellChecker (one per application).
+ (NSSpellChecker *)sharedSpellChecker
The spelling checker shared by this application.
NSSpellChecker.hReturns whether the application’s NSSpellChecker has already been created.
+ (BOOL)sharedSpellCheckerExists
YES if the shared spell checker already exists, otherwise NO.
NSSpellChecker.hReturns a guaranteed unique tag to use as the spell-document tag for a document.
+ (NSInteger)uniqueSpellDocumentTag
Returns a unique tag to identified this spell checked object.
Use this method to generate tags to avoid collisions with other objects that can be spell checked.
NSSpellChecker.hReturns the Spelling panel’s accessory view.
- (NSView *)accessoryView
The accessory view or nil if there is none.
NSSpellChecker.hReturns whether the spell checker will automatically identify languages.
- (BOOL)automaticallyIdentifiesLanguages
YES if languages are automatically identified, otherwise NO.
NSSpellChecker.hProvides a list of all available languages.
- (NSArray *)availableLanguages
An array containing all the available spell checking languages. The languages are ordered in the user’s preferred order as set in the system preferences.
If automaticallyIdentifiesLanguages is YES, then text checking will automatically use this method as appropriate; otherwise, it will use the language set by setLanguage:.
The older checkSpellingOfString:startingAt:language:wrap:inSpellDocumentWithTag:wordCount: and checkGrammarOfString:startingAt:language:wrap:inSpellDocumentWithTag:details:. methods will use the language set by setLanguage:, if they are called with a nil language argument.
NSSpellChecker.hInitiates a grammatical analysis of a given string.
- (NSRange)checkGrammarOfString:(NSString *)string startingAt:(NSInteger)start language:(NSString *)language wrap:(BOOL)wrap inSpellDocumentWithTag:(NSInteger)documentTag details:(NSArray **)outDetails
String to analyze.
Location within string at which to start the analysis.
Language use in string. When nil, the language selected in the Spelling panel is used.
YES to specify that the analysis continue to the beginning of string when the end is reached.
NO to have the analysis stop at the end of string.
An identifier unique within the application used to inform the spell checker which document that text is associated, potentially for many purposes, not necessarily just for ignored words. A value of 0 can be passed in for text not associated with a particular document.
On output, dictionaries describing grammar-analysis details within the flagged grammatical unit. See the NSSpellServer class for information about these dictionaries.
Location of the first flagged grammatical unit.
NSSpellChecker.hStarts the search for a misspelled word in stringToCheck starting at startingOffset within the string object.
- (NSRange)checkSpellingOfString:(NSString *)stringToCheck startingAt:(NSInteger)startingOffset
The string to spell check.
The offset at which to start checking.
Returns the range of the first misspelled word.
Wrapping occurs, but no ignored-words dictionary is used.
NSSpellChecker.hStarts the search for a misspelled word in a string starting at specified offset within the string.
- (NSRange)checkSpellingOfString:(NSString *)stringToCheck startingAt:(NSInteger)startingOffset language:(NSString *)language wrap:(BOOL)wrapFlag inSpellDocumentWithTag:(NSInteger)tag wordCount:(NSInteger *)wordCount
The string object containing the words to spellcheck.
The offset within stringToCheck at which to begin spellchecking.
The language of the words in the string. If language is nil, or if you obtain the value by sending language to self, the current selection in the Spelling panel’s pop-up menu is used. Do not pass in an empty string for language.
YES to indicate that spell checking should continue at the beginning of the string when the end of the string is reached; NO to indicate that spellchecking should stop at the end of the document.
An identifier unique within the application used to inform the spell checker which document that text is associated, potentially for many purposes, not necessarily just for ignored words. A value of 0 can be passed in for text not associated with a particular document.
Returns by indirection a count of the words spell-checked up to and including the first error (if any), or -1 if the spell checker fails or does not support word counting. Specify NULL if you do not want this word count.
The range of the first misspelled word and optionally (and by reference) the count of words spellchecked in the string in wordCount.
NSSpellChecker.hRequests unified text checking for the given range of the given string.
- (NSArray *)checkString:(NSString *)stringToCheck range:(NSRange)range types:(NSTextCheckingTypes)checkingTypes options:(NSDictionary *)options inSpellDocumentWithTag:(NSInteger)tag orthography:(NSOrthography **)orthography wordCount:(NSInteger *)wordCount
The string to check.
The range of the string to check.
The type of checking to be performed. The possible constants are listed in NSTextCheckingType and can be combined using the C bit-wise OR operator to perform multiple checks at the same time.
The options dictionary specifying the types of checking to perform. See “Spell Checking Option Dictionary Keys” for the possible keys and expected values.
An identifier unique within the application used to inform the spell checker which document that text is associated, potentially for many purposes, not necessarily just for ignored words. A value of 0 can be passed in for text not associated with a particular document.
Returns by-reference, the orthography of the range of the string. See NSOrthography for more information.
Returns by-reference, the word count for the range of the string.
An array of NSTextCheckingResult objects describing particular items found during checking and their individual ranges, sorted by range origin, then range end, then result type.
NSSpellChecker.hNotifies the receiver that the user has finished with the tagged document.
- (void)closeSpellDocumentWithTag:(NSInteger)tag
The spell checker will release any resources associated with the document, including but not necessarily limited to, ignored words.
NSSpellChecker.hProvides a list of complete words that the user might be trying to type based on a partial word in a given string.
- (NSArray *)completionsForPartialWordRange:(NSRange)partialWordRange inString:(NSString *)string language:(NSString *)language inSpellDocumentWithTag:(NSInteger)spellDocumentTag
Range that identifies a partial word in string.
String with the partial word from which to generate the result.
Language to used in string. When nil, this method uses the language selected in the Spelling panel.
Identifies the spell document with ignored words to use.
List of complete words from the spell checker dictionary in the order they should be presented to the user.
NSSpellChecker.hReturns the number of words in stringToCount.
- (NSInteger)countWordsInString:(NSString *)stringToCount language:(NSString *)language
The string to count the words in.
The language of the string.
The number of words in the string.
If language is nil, the current selection in the Spelling panel’s pop-up menu is used.
NSSpellChecker.hReturns an array of possible substitutions for the specified string.
- (NSArray *)guessesForWordRange:(NSRange)range inString:(NSString *)string language:(NSString *)language inSpellDocumentWithTag:(NSInteger)tag
The range of the string to check.
The string to guess
The language of the string
An identifier unique within the application used to inform the spell checker which document that text is associated, potentially for many purposes, not necessarily just for ignored words. A value of 0 can be passed in for text not associated with a particular document.
An array of strings containing possible replacement words.
NSSpellChecker.hIndicates whether the spell checker has learned a given word.
- (BOOL)hasLearnedWord:(NSString *)word
Word in question.
NSSpellChecker.hReturns the array of ignored words for a document identified by tag.
- (NSArray *)ignoredWordsInSpellDocumentWithTag:(NSInteger)tag
Invoke this method before closeSpellDocumentWithTag: if you want to store the ignored words.
NSSpellChecker.hInstructs the spell checker to ignore all future occurrences of wordToIgnore in the document identified by tag.
- (void)ignoreWord:(NSString *)wordToIgnore inSpellDocumentWithTag:(NSInteger)tag
You should invoke this method from within your implementation of the NSIgnoreMisspelledWords protocol’s ignoreSpelling: method.
NSSpellChecker.hReturns the current language used in spell checking.
- (NSString *)language
The current spell checking language, as a string.
The result string specifies the language using the language and regional designations described in Language and Locale Designations in Internationalization Programming Topics.
NSSpellChecker.hAdds the word to the spell checker dictionary.
- (void)learnWord:(NSString *)word
The word to add.
NSSpellChecker.hProvides a menu containing contextual menu items suitable for certain kinds of detected results.
- (NSMenu *)menuForResult:(NSTextCheckingResult *)result string:(NSString *)checkedString options:(NSDictionary *)options atLocation:(NSPoint)location inView:(NSView *)view
The NSTextCheckingResult instance for the checked string.
The string that has been checked.
The options dictionary allows clients to pass in information associated with the document. See “Spell Checking Option Dictionary Keys” for possible key-value pairs.
The location, in the view’s coordinate system, to display the menu.
The view object over which to display the contextual menu.
A menu suitable for displaying as a contextual menu, or adding to another contextual menu as a submenu.
NSSpellChecker.hRequests that the string be checked in the background.
- (NSInteger)requestCheckingOfString:(NSString *)stringToCheck range:(NSRange)range types:(NSTextCheckingTypes)checkingTypes options:(NSDictionary *)options inSpellDocumentWithTag:(NSInteger)tag completionHandler:(void (^)(NSInteger sequenceNumber, NSArray *results, NSOrthography *orthography, NSInteger wordCount))completionHandler
The string to check.
The range of the string to check.
The type of checking to be performed. The possible constants are listed in NSTextCheckingType and can be combined using the C bit-wise OR operator to perform multiple checks at the same time.
The options dictionary specifying the types of checking to perform. See “Spell Checking Option Dictionary Keys” for the possible keys and expected values.
An identifier unique within the application used to inform the spell checker which document that text is associated, potentially for many purposes, not necessarily just for ignored words. A value of 0 can be passed in for text not associated with a particular document.
The completion handler block object will be called (in an arbitrary context) when results are available, with the sequence number and results.
The block takes four arguments:
A monotonically increasing sequence number.
An array of NSTextCheckingResult objects describing particular items found during checking and their individual ranges, sorted by range origin, then range end, then result type..
The orthography of the string.
The number of words in the range of the string.
The return value is a monotonically increasing sequence number that can be used to keep track of requests in flight.
NSSpellChecker.hMakes an view an accessory of the Spelling panel by making it a subview of the panel’s content view.
- (void)setAccessoryView:(NSView *)aView
The accessory view displayed in the receiver.
The accessory view can be any custom view you want to display with the spelling panel. The accessory view is displayed below the spelling checker and the panel automatically resizes to accommodate the accessory view.
This method posts a notification named NSWindowDidResizeNotification with the Spelling panel object to the default notification center.
NSSpellChecker.hSets whether the spell checker will automatically identify languages.
- (void)setAutomaticallyIdentifiesLanguages:(BOOL)flag
YES if languages should be automatically identified, otherwise NO.
NSSpellChecker.hInitializes the ignored-words document (a dictionary identified by tag with someWords), an array of words to ignore.
- (void)setIgnoredWords:(NSArray *)someWords inSpellDocumentWithTag:(NSInteger)tag
NSSpellChecker.hReturns whether the specified language is in the Spelling pop-up list.
- (BOOL)setLanguage:(NSString *)language
The requested language.
YES if the language is available in the pop-up list, otherwise NO.
Listing 1 shows how languages can be specified in language. If the language specified is listed in the user’s list of preferred languages, the spell checker uses that language to accomplish its task.
Listing 1 Specifying the spell checker language
NSSpellChecker* spell_checker = [NSSpellChecker sharedSpellChecker]; |
// Sets language to French. The language method returns "fr". |
[spell_checker setLanguage:@"fr"]; |
// Sets language to the one spoken in Netherlands (English). The language method returns "en". |
[spell_checker setLanguage:@"NL"]; |
// Sets language to British English. The language method returns "en_GB". |
[spell_checker setLanguage:@"en_GB"] |
// Sets language to German. The language method returns "de". |
[spell_checker setLanguage:@"German"]; |
To learn about the strings you can use to specify a language in language, see Language and Locale Designations in Internationalization Programming Topics.
NSSpellChecker.hSets the substitutions panel’s accessory view.
- (void)setSubstitutionsPanelAccessoryViewController:(NSViewController *)accessoryController
The accessory view controller or nil if there is none.
The accessory view controller can accommodate be any custom view you want to display with the substitutions panel. The accessory view controller’s view is displayed below the substitutions list and the panel automatically resizes to accommodate the accessory view.
This method posts a notification named NSWindowDidResizeNotification with the substitutions panel object to the default notification center.
NSSpellChecker.hSets the string that appears in the misspelled word field, using the string object aString.
- (void)setWordFieldStringValue:(NSString *)aString
NSSpellChecker.hReturns the spell checker’s panel.
- (NSPanel *)spellingPanel
The spell checking panel.
NSSpellChecker.hReturns the substitutions panel.
- (NSPanel *)substitutionsPanel
The substitutions checking panel.
NSSpellChecker.hReturns the substitutions panel’s accessory view controller.
- (NSViewController *)substitutionsPanelAccessoryViewController
The accessory view controller or nil if there is none.
NSSpellChecker.hTells the spell checker to unlearn a given word.
- (void)unlearnWord:(NSString *)word
Word to unlearn.
NSSpellChecker.hUpdates the available panels to account for user changes.
- (void)updatePanels
This method should be called when a client changes some relevant setting, such as what kind of spelling, grammar checking, or substitutions it uses.
NSSpellChecker.hSpecifies a grammar-analysis detail to highlight in the Spelling panel.
- (void)updateSpellingPanelWithGrammarString:(NSString *)problemString detail:(NSDictionary *)detail
Problematic grammatical unit identified by checkGrammarOfString:startingAt:language:wrap:inSpellDocumentWithTag:details:.
One of the grammar-analysis details provided by checkGrammarOfString:startingAt:language:wrap:inSpellDocumentWithTag:details:.
NSSpellChecker.hCauses the spell checker to update the Spelling panel’s misspelled-word field to reflect word.
- (void)updateSpellingPanelWithMisspelledWord:(NSString *)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.
NSSpellChecker.hProvides a subset of the available languages to be used for spell checking.
- (NSArray *)userPreferredLanguages
An array containing the user’s preferred languages for spell checking. The order is set in the system preferences.
If automaticallyIdentifiesLanguages is YES, then text checking will automatically use this method as appropriate; otherwise, it will use the language set by setLanguage:.
The older checkSpellingOfString:startingAt:language:wrap:inSpellDocumentWithTag:wordCount: and checkGrammarOfString:startingAt:language:wrap:inSpellDocumentWithTag:details:. methods will use the language set by setLanguage:, if they are called with a nil language argument.
NSSpellChecker.hReturns the default values for quote replacement.
- (NSArray *)userQuotesArrayForLanguage:(NSString *)language
The language for quote replacement.
An array of quote replacements used by the NSTextCheckingQuotesKey key-value pair.
NSSpellChecker.hReturns the dictionary used when replacing words.
- (NSDictionary *)userReplacementsDictionary
The dictionary.
The key-value pairs in this dictionary are used by the NSTextCheckingQuotesKey when replacing characters and words.
NSSpellChecker.hThe constants are optional keys that can be used in the options dictionary parameter of the checkString:range:types:options:inSpellDocumentWithTag:orthography:wordCount:, requestCheckingOfString:range:types:options:inSpellDocumentWithTag:completionHandler:, and menuForResult:string:options:atLocation:inView: methods.
NSString *NSTextCheckingOrthographyKey; NSString *NSTextCheckingQuotesKey; NSString *NSTextCheckingReplacementsKey; NSString *NSTextCheckingReferenceDateKey; NSString *NSTextCheckingReferenceTimeZoneKey; NSString *NSTextCheckingDocumentURLKey; NSString *NSTextCheckingDocumentTitleKey; NSString *NSTextCheckingDocumentAuthorKey;
NSTextCheckingOrthographyKeyAn NSOrthography instance indicating an orthography to be used as a starting point for orthography checking, or as the orthography if orthography checking is not enabled.
Available in Mac OS X v10.6 and later.
Declared in NSSpellChecker.h.
NSTextCheckingQuotesKeyAn NSArray containing four strings to be used with NSTextCheckingTypeQuote (opening double quote, closing double quote, opening single quote, and closing single quote in that order); if not specified, values will be taken from user's preferences.
Available in Mac OS X v10.6 and later.
Declared in NSSpellChecker.h.
NSTextCheckingReplacementsKeyAn NSDictionary containing replacements to be used with NSTextCheckingTypeReplacement; if not specified, values will be taken from user's preferences.
Available in Mac OS X v10.6 and later.
Declared in NSSpellChecker.h.
NSTextCheckingReferenceDateKeyAn NSDate to be associated with the document, used as a referent for relative dates; if not specified, the current date will be used.
Available in Mac OS X v10.6 and later.
Declared in NSSpellChecker.h.
NSTextCheckingReferenceTimeZoneKeyAn NSTimeZone to be associated with the document, used as a reference for dates without time zones; if not specified, the current time zone will be used.
Available in Mac OS X v10.6 and later.
Declared in NSSpellChecker.h.
NSTextCheckingDocumentURLKeyAn NSURL to be associated with the document.
Available in Mac OS X v10.6 and later.
Declared in NSSpellChecker.h.
NSTextCheckingDocumentTitleKeyAn NSString containing the title to be associated with the document.
Available in Mac OS X v10.6 and later.
Declared in NSSpellChecker.h.
NSTextCheckingDocumentAuthorKeyAn NSString containing the name of an author to be associated with the document
Available in Mac OS X v10.6 and later.
Declared in NSSpellChecker.h.
Last updated: 2009-07-08