How to reset attributes in NSTextStorage

607 views Asked by At

I am trying to a text parser for iOS. I have some syntax to bold, italic, highlight, etc. I have set it up in a UITextView with an instance of NSTextStorage to monitor the changes and set the correct attributes. If I preload the UITextView with text it works great.

However when I'm typing and let's say I type something like this

==a==

It works great it highlights it yellow and everything is fine. However if I place the cursor right next to the last == and starting typing to create something like this:

==a==hello my name is...

Then the rest of the text also gets highlighted.

I know the regex I use is not the problem because if I pre fill the UITextView with that text it parses it correctly.

Question: So what I need help with is resetting the the current cursor position to no text attributes. I'm not sure how to do that because if it is at the end of the text view I can't go forward because then I get an NSRangeException.

Thanks!

1

There are 1 answers

0
Jason Silberman On BEST ANSWER

I figured this out, what you have to do is in the delegate for UITextView implement the textViewDidChange: method and set the typingAttributes.

Like this:

- (void)textViewDidChange:(UITextView *)textView {
    textview.typingAttributes = @{...};
}

Now every time the text updates it will reset to the default attributes.