2 Replies Latest reply: Nov 19, 2012 11:11 AM by araghur
araghur Level 1 (0 points)

I am trying to create my custom spell checker for the UITextView.

To show that a word is misspelled, I need to add the "red dotted line" under the misspelled word. To do that, here is what I thought would work -

I create a dictionary which contains the values for the key NSUnderlineStyleAttributeName. However, what this does is, it does underline the characters but it does not have a dotted pattern. Also, setting the strokeColorAttribute also does not seem to have any effect.

Here is my code -

NSMutableDictionary *misspelledAttributes = [NSMutableDictionary dictionary];

[misspelledAttributes setObject:[NSNumber numberWithInt:kCTUnderlineStyleThick|kCTUnderlinePatternDot] forKey:NSUnderlineStyleAttributeName];

[misspelledAttributes setObject:[UIColor redColor] forKey:NSStrokeColorAttributeName];

To set the attributes to a attributed string for a particular range -

NSMutableAttributedString *attrString = [self.textView.attributedText mutableCopy];

[attrString addAttributes:misspelledAttributes range:wordRange];

self.textView.scrollEnabled = NO;

self.textView.attributedText = attrString;

self.textView.selectedRange = NSMakeRange(NSMaxRange(range), 0);

self.textView.scrollEnabled = YES;

  • Jongware Level 2 (265 points)

    You seem to be changing the applied stroke of the text itself (and perhaps it's notshowing because it doesn't have a color).

    Try changing the underline style and pattern, as in this sample, found through Google: http://cocoadev.com/wiki/CustomSpellingView

  • araghur Level 1 (0 points)

    I agree that the NSStrokeColorAttributeName is not the correct property to use. I am not able to edit my post to remove that piece of code.


    I am trying to do this for my iOS project and NSUnderlineColorAttributeName is not available for iOS yet. It is only available for OSX. I just checked that in the developer library.

    Am I missing something here?


    Also, I am unable to add a "dotted" underline. All I can do is put a single line under the text. is Apple allowing us to only put a single line and not any of the other types of underline styles such as dashed, dotted etc.?