[英]How to reset attributes in NSTextStorage
I am trying to a text parser for iOS. 我正在尝试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. 我有一些粗体,斜体,高亮等语法。我已经在
UITextView
设置了NSTextStorage
实例来监控更改并设置正确的属性。 If I preload the UITextView
with text it works great. 如果我使用文本预加载
UITextView
,它可以很好地工作。
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. 我知道我使用的正则表达式不是问题,因为如果我预先用该文本填充
UITextView
,它会正确解析它。
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
. 我不知道该怎么做,因为如果它在文本视图的末尾我不能继续前进,因为那时我得到一个
NSRangeException
。
Thanks! 谢谢!
I figured this out, what you have to do is in the delegate
for UITextView
implement the textViewDidChange:
method and set the typingAttributes
. 我想通了,你要做的是在
UITextView
的delegate
中实现typingAttributes
textViewDidChange:
方法并设置typingAttributes
。
Like this: 像这样:
- (void)textViewDidChange:(UITextView *)textView {
textview.typingAttributes = @{...};
}
Now every time the text updates it will reset to the default attributes. 现在,每次文本更新时,它都将重置为默认属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.