3

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!

4

1 回答 1

2

我想通了,你要做的是在delegateforUITextView实现textViewDidChange:方法并设置typingAttributes.

像这样:

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

现在每次文本更新时,它都会重置为默认属性。

于 2014-11-28T20:57:51.033 回答