当我将文本粘贴到 NSTextView 时,如何仅粘贴纯文本?
当我将文本粘贴到 NSTextView 时,我希望只能粘贴纯文本。应删除所有富文本格式,包括:字体、颜色、链接和段落样式。粘贴的所有文本应以文本视图的默认字体和样式显示。 NSTextView默认接受富文本,如何禁用它?
When I paste text to a NSTextView, I wish I can paste plain text only. All the rich text formats should be removed, include: font, color, link, and paragraph style. All the text pasted should be displayed with the default font and style of the text view. NSTextView accepts rich text by default, how to disable it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
isRichText = false
禁用丰富文本。Use
isRichText = false
to disable rich text.在您的 NSTextView 中重写此方法:
对我来说,这对于粘贴和拖放都有效。
Override this method in your
NSTextView
:For me this worked both for pasting and drag-and-drop.
使用以下方法定义自定义
NSTextView
类:注意:从 Mac OS X 开始,粘贴板类型的新
typedef
给出为NSPasteboardTypeString
而不是NSStringPBoardType
:Define a custom
NSTextView
class with following method:Note: As of Mac OS X the new
typedef
for the pasteboard type is given asNSPasteboardTypeString
instead ofNSStringPBoardType
:上述解决方案确实解决了发问者粘贴文本的问题,但我认为发问者可能想要的不止于此。至少当我来到这里的时候我是这么做的。
我只是希望文本字段中的所有字符始终具有相同的字体,无论它们是通过编程方式插入、从笔尖插入、粘贴、拖入、键入还是由圣诞老人放入克劳斯。我在 Stack Overflow 上搜索了这个更广泛的问题,但没有找到任何问题(或答案)。
而不是此处给出的解决方案使用这个想法。详细来说,给文本字段一个实现这个的委托......
完成。这适用于我能想到测试的所有边缘情况。观察到这个选择的变化有点奇怪。似乎观察视图文本对象的字符串值,或者注册 NSTextDidChangeNotification 会更合乎逻辑,但由于我已经设置了委托,并且由于上面的内容已经过测试并得到了 Nick Zitzmann 的赞许,所以我去了与它。
The above solutions do resolve the questioner's issue of pasted-in text, but I think that the questioner probably wanted more than that. At least I did when I came here.
I simply want all the characters in my text field to always have the same font, regardless of whether they are inserted programatically, from the nib, pasted in, dragged in, typed in, or dropped in by Santa Claus. I searched Stack Overflow for this broader issue but did not find any questions (or answers).
Instead of the solutions given here, use this idea. In detail, give the text field a delegate which implements this…
Done. This works for all of the edge cases I could think of to test. It's a little weird, to observe a change in the selection for this. Seems like observing the string value of the view's text object, or registering for NSTextDidChangeNotification, would be more logical, but since I already had a delegate set up, and since the above was tested and given the thumbs-up by Nick Zitzmann, I went with it.