cocoacopy-pastenstextview

When I paste text to a NSTextView, how to paste plain text only?


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?


Solution

  • Define a custom NSTextView class with following method:

    - (NSArray *)readablePasteboardTypes {
        return [NSArray arrayWithObjects:NSStringPboardType, 
                nil];
    }
    

    Note: As of Mac OS X the new typedef for the pasteboard type is given as NSPasteboardTypeString instead of NSStringPBoardType:

    - (NSArray *)readablePasteboardTypes {
        return [NSArray arrayWithObjects:NSPasteboardTypeString,
                nil];
    }