Hello,
I have an NSTextView subclass that provides a custom contextual menu by
overriding -menuForEvent:. Because I override this, I have to provide any menu
items I want to retain from the original menu myself. Mostly, that's not a
problem, but I want to keep the spell checking options at the top of the menu
as well. I thought I had this covered. This is what I'm doing:
// Is there a selection?
if (selRange.length > 0 && selRange.location < [text length])
{
// If so, check to see if the selected range constitutes a misspelled
word.
NSSpellChecker *spellChecker = [NSSpellChecker sharedSpellChecker];
NSRange misspelledRange = [spellChecker checkSpellingOfString:[[text
string] substringWithRange:selRange] startingAt:0];
// Detected misspelled word?
if (misspelledRange.length == selRange.length)
{
// Get suggestions.
NSArray *suggestions = [spellChecker guessesForWord:[[text
string] substringWithRange:selRange]];
// Are there any suggestions?
if ([suggestions count] > 0)
{
// If so, add them to the menu with an appropriate
action.
}
else
{
// Otherwise insert the "No Guesses Found" item.
}
}
I thought all of this was working fine. However, a user has just pointed out to
me that it doesn't work for all misspellings... Which is very strange. The
problem comes down to NSSpellChecker's -checkSpellingOfString:. This returns an
NSNotFound range for certain misspellings.
For instance:
Try typing "accade" into TextEdit (I'm assuming English as the language here,
of course). It is underlined in red, and ctrl-clicking on it brings up a list
of suggestions. So the system recognises it as a misspelling, a word that it
doesn't know.
Now try this in any test app:
NSRange range = [[NSSpellChecker sharedSpellChecker]
checkSpellingOfString:@"accade" startingAt:0];
NSLog (@"NSStringFromRange(range));
range will be (NSNotFound,0).
I don't understand why, though. Why isn't NSSpellChecker returning this as a
misspelling? Am I missing something obvious? Is there a better way to insert
the spelling suggestions at the top of the menu?
Thanks and all the best,
Keith
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]