When I embed text in an NSTextView, how to embed only plain text?

When I paste text into an NSTextView, I want me to be able to paste only plain text. All formatted text formats should be deleted: font, color, link, and paragraph style. All inserted texts should be displayed in font and default style for the text view. NSTextView accepts rich text by default, how to disable it?

+7
source share
4 answers

Define a custom NSTextView class with the following method:

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

Note Starting with Mac OS X, the new typedef for the circuit board type is indicated as NSPasteboardTypeString instead of NSStringPBoardType :

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

Use setRichText: to disable setRichText: text.

+9
source

Cancel this method in an NSTextView :

 - (NSString *)preferredPasteboardTypeFromArray:(NSArray *)availableTypes restrictedToTypesFromArray:(NSArray *)allowedTypes { if ([availableTypes containsObject:NSPasteboardTypeString]) { return NSPasteboardTypeString; } return [super preferredPasteboardTypeFromArray:availableTypes restrictedToTypesFromArray:allowedTypes]; } 

For me, this worked for both insert and drag.

+3
source

The above solutions eliminate the question of the inserted text, but I think the questionnaire probably wanted more. At least I did when I came here.

I just want all the characters in my text box to always have the same font , regardless of whether they are inserted programmatically, from anywhere, inserted, dragged, entered or deleted in Santa Claus. I searched for qaru 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 box a delegate that implements this ...

 - (void)textViewDidChangeSelection:(NSNotification *)note { NSTextView* textView = [note object] ; [textView setFont:[self fontIWant]] ; } 

Done. This works in all cases that I could come up with for testing. It's a little strange to watch a change of choice for this. It seems like observing the string value of a text object of a view or registering for NSTextDidChangeNotification would be more logical, but since I already had a delegate set up and since the above was checked and thumbs-up by Nick Zitzmann, I went with it.

+2
source

All Articles