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?
4 Answers
Use isRichText = false
to disable rich text.

- 5,469
- 4
- 30
- 41

- 115,675
- 35
- 233
- 266
-
But I need to add attributes to the text in the text view. I only want to remove rich text format from the pasted text. – Stephen Hsu Aug 18 '11 at 03:31
-
6You could try subclassing to override `-paste:` and call `-pasteAsPlainText:` instead. I'm not sure if that will work. – jtbandes Aug 18 '11 at 03:34
Override this method in your NSTextView
:
- (NSString *)preferredPasteboardTypeFromArray:(NSArray *)availableTypes restrictedToTypesFromArray:(NSArray *)allowedTypes {
if ([availableTypes containsObject:NSPasteboardTypeString]) {
return NSPasteboardTypeString;
}
return [super preferredPasteboardTypeFromArray:availableTypes restrictedToTypesFromArray:allowedTypes];
}
For me this worked both for pasting and drag-and-drop.

- 479
- 5
- 7
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];
}

- 12,479
- 14
- 66
- 123

- 5,127
- 7
- 31
- 39
-
1This has the same effect as calling `setRichText:NO`, and while it will convert pasted rich text into plain text, it will also disable reception of drag-and-drop for rich text. Better, I think, to override `paste:` and do your own conversion (or even, as @jtbandes suggests, just call `pasteAsPlainText:`. – zpasternack Oct 15 '13 at 22:39
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…
- (void)textViewDidChangeSelection:(NSNotification *)note {
NSTextView* textView = [note object] ;
[textView setFont:[self fontIWant]] ;
}
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.

- 4,860
- 33
- 39
-
I don't think this is a good solution for most cases. It doesn't seem necessary to change the font every time the cursor moves position and, for me, the text shifts slightly whenever it does. – danjonweb Jul 28 '14 at 21:04