如何截取粘贴到NSTextView以删除不支持的格式化? [英] How can you intercept pasting into a NSTextView to remove unsupported formatting?

查看:595
本文介绍了如何截取粘贴到NSTextView以删除不支持的格式化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为简单的WYSIWYG编辑创建一个简单的基于NSTextView的窗口。但是,我只想允许某些类型的格式化(例如粗体,斜体,下划线和单一标题类型,但没有颜色或不同的字体。)



问题是if我只是使用NSTextView,有人可以创建或复制格式文本在另一个程序,然后只需将其粘贴到该视图,所有的格式化与它,允许我不允许的东西,如不同的字体,颜色等。 p>

最好,我想自动去掉我的应用程序不支持的任何格式。在最坏的情况下,我想简单地截取粘贴,并将其更改为纯文本,但他们必须手动重新格式化。



注意:这里多次提到类似的问题,但是他们通常指的是网络或使用JavaScript / JQuery。我特别指的是在Mac应用程序中使用NSTextView,所以请在将其标记为重复之前,确保这是其他问题所指的。


解决方案



一个可能的解决方案是让你的NSTextView实现这个模板方法:

   - void)paste:(id)sender {
NSPasteboard * pb = [NSPasteboard generalPasteboard];
//从粘贴板接收格式化的字符串
//从字符串中删除格式化
//将明文字符串放回粘贴板
[super paste:sender];
//放回初始格式化的字符串
}

不必处理任何实际的插入/粘贴,但可能在实际粘贴之前粘贴板。



您可能还想查看 NSTextView 处理Pasteboard:




  • preferredPasteboardTypeFromArray:restrictedToTypesFromArray:

  • readSelectionFromPasteboard:type:
  • readSelectionFromPasteboard:

  • readablePasteboardTypes

  • writablePasteboardTypes

  • writeSelectionToPasteboard:type:

  • writeSelectionToPasteboard:types:

  • validRequestorForSendType:returnType:

    I'm trying to create a simple NSTextView-based window for simple WYSIWYG editing. However, I only want to allow certain types of formatting (e.g. Bold, Italic, Underline and a single heading type but no colors or different fonts.)

    The issue is if I simply use NSTextView, someone can create or copy formatted text in another program, then simply paste it into that view and all that formatting goes with it, allowing things I'm not allowing, such as different fonts, colors, etc.

    At best, I want to automatically strip out any formatting that my application doesn't support. At worst, I want to simply intercept the paste and change it to plain-text although they would have to then manually re-format it. But that's preferable to invalid formatting.

    Note: Something similar has been asked here on SO multiple times, but they're usually referring to the web or using JavaScript/JQuery. I am specifically referring to using NSTextView in a Mac app so please, before simply marking this as a duplicate, make sure that's what the other question refers to. Thank you.

    解决方案

    [Edit: see Joshua Nozzi's comment!]

    One possible solution would be to make your NSTextView implement this template method:

    - (void)paste:(id)sender {
        NSPasteboard *pb = [NSPasteboard generalPasteboard];
        //receive formatted string from pasteboard
        //remove formatting from string
        //put back plaintext string into pasteboard
        [super paste:sender];
        //put back initial formatted string
    }
    

    This way you don't have to handle any of the actual insertion/pasting but can mess with the pasteboard before the actual pasting.

    You might also want to look into these methods of NSTextView dealing with the Pasteboard:

    • preferredPasteboardTypeFromArray:restrictedToTypesFromArray:
    • readSelectionFromPasteboard:
    • readSelectionFromPasteboard:type:
    • readablePasteboardTypes
    • writablePasteboardTypes
    • writeSelectionToPasteboard:type:
    • writeSelectionToPasteboard:types:
    • validRequestorForSendType:returnType:

    这篇关于如何截取粘贴到NSTextView以删除不支持的格式化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆