Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ashwinr87

macrumors member
Original poster
Mar 9, 2011
81
0
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 -

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 -

Code:
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;

It would be great if someone could help me with this and point out what I am missing/doing wrong.
 
Last edited:
To set the attributes to a attributed string for a particular range -

Code:
NSMutableAttributedString *attrString = [self.textView.attributedText mutableCopy];
[attrString addAttributes:misspelledAttributes range:wordRange];

It would be great if someone could help me with this and point out what I am missing/doing wrong.

You take the string from your text view, copy it, modify the copy, but from what we can see, you never do anything with that copy afterwards.

I'd suggest adding a line along the lines of

Code:
self.textView.attributedText = attrString;

to the end of what you showed us and see how that works.
 
sorry.. probably I was not clear enough.. I do replace the string in the text view after adding my attributes. That was not the problem I was facing.

The problem I am facing is that, I am not able to add dotted red lines. All I am able to add is a thick single line.

You take the string from your text view, copy it, modify the copy, but from what we can see, you never do anything with that copy afterwards.

I'd suggest adding a line along the lines of

Code:
self.textView.attributedText = attrString;

to the end of what you showed us and see how that works.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.