搜索具有自定义属性的 NSAttributedString 并将其删除?怎样做?
我在 NSTextView 中有一个占位符字符串,一个空格字符,我想在将来删除它。我的策略是为 NSAttributedString 分配一个自定义属性,并在 [NSTextView string]
上执行搜索以删除它。到目前为止,除了通过 [NSTextStorage attributeRuns]
之外,我还没有找到执行此操作的好方法。有人对此有什么好主意吗?
谢谢!
I have a placeholder string, a space character, in an NSTextView that I want to remove at a future time. My strategy is to assign the NSAttributedString a custom attribute and perform a search on the [NSTextView string]
removing it. So far, I haven't found a good way to do this, outside of going through [NSTextStorage attributeRuns]
. Anyone have a good idea for this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看我编写的自定义 NSAttributedString 属性的示例实现: http://aimannajjar.com/blog/1-How-to-Create-NSTextView-with-Facebook-like-Tags-Mentions.html。
基本上,我使用
- enumerateAttributesInRange:options:usingBlock:
循环遍历所有属性,然后我有一个自定义绘图逻辑,仅在属性为“Tag”时运行。Check out this example implementation that I wrote of custom NSAttributedString attribute: http://aimannajjar.com/blog/1-How-to-Create-NSTextView-with-Facebook-like-Tags-Mentions.html.
Basically, I loop though all attributes using
- enumerateAttributesInRange:options:usingBlock:
and then I have a custom drawing logic that only runs when the attribute is "Tag".Apple 的 AttributedString 编程指南 提供了示例,展示了如何使用 attribute:atIndex: effectiveRange: 和配套方法循环遍历属性字符串。 10.6 也有一个枚举器块。
Apple's AttributedString Programming Guide has examples showing how to loop through an attributed string using attribute:atIndex:effectiveRange: and companion methods. There's an enumerator block, too, for 10.6.