【问题标题】:Accepting TextClipping with NSImageView Drop Box使用 NSImageView Drop Box 接受 TextClipping
【发布时间】:2013-05-21 21:33:22
【问题描述】:

如果您将选定的文本拖放到文件夹中,您将获得一个扩展名为 textClipping 的文件。 TextEdit 的文档窗口接受文本选择。许多应用程序确实接受 textClipping。您如何在 NSImageView 下拉框中接收文本选择? performDragOperation 的常规操作似乎不接受文本选择。

- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
NSPasteboard *pboard = [sender draggingPasteboard];
NSArray *urls;
    if ([[pboard types] containsObject:NSURLPboardType]) {
        urls = [pboard readObjectsForClasses:@[[NSURL class]] options:nil];
    }

    AppDelegate *appDelegate = (AppDelegate *)[NSApp delegate];
    ...
    ...

    return YES;
}

这些代码行让我可以接受文件,但不能接受 textClipping。接受 textClipping 的秘诀是什么?也许,你不能用 NSImageView 接受它?使用“Objective-C textClipping”运行搜索没有任何结果。

感谢您的建议。

【问题讨论】:

    标签: objective-c macos text osx-mountain-lion nsimageview


    【解决方案1】:

    文本剪辑可以是字符串或属性字符串(如果内容包含富文本)。
    要从粘贴板中读取这些对象,您必须分别搜索 NSStringPboardTypeNSRTFPboardType

    NSStringPboardType 可以读作NSString
    NSRTFPboardType 可以读作NSAttributedString

    - (BOOL)performDragOperation:(id<NSDraggingInfo>)sender
    {
        NSPasteboard* pboard = [sender draggingPasteboard];
        NSArray* pboardContents = nil;
        if ([[pboard types] containsObject:NSURLPboardType])
        {
            pboardContents = [pboard readObjectsForClasses:@[[NSURL class]] options:nil];
        }
        if ([[pboard types] containsObject:NSStringPboardType])
        {
            pboardContents = [pboard readObjectsForClasses:@[[NSString class]] options:nil];
        }
        if ([[pboard types] containsObject:NSRTFPboardType])
        {
            pboardContents = [pboard readObjectsForClasses:@[[NSAttributedString class]] options:nil];
        }
        NSLog(@"Pasteboard contents:%@", pboardContents);
        return YES;
    }
    

    【讨论】:

    • 谢谢你,weichsel。带有此代码的 NSImageView 下拉框似乎不接受文本下拉。我做错了什么,你知道吗?
    • 其实,我似乎可以只使用一个NSTextView控件来接受一个文本放置。
    猜你喜欢
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多