当我将文本粘贴到 NSTextView 时,如何仅粘贴纯文本?

发布于 2024-11-30 04:27:30 字数 117 浏览 1 评论 0原文

当我将文本粘贴到 NSTextView 时,我希望只能粘贴纯文本。应删除所有富文本格式,包括:字体、颜色、链接和段落样式。粘贴的所有文本应以文本视图的默认字体和样式显示。 NSTextView默认接受富文本,如何禁用它?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

橘香 2024-12-07 04:27:30

使用 isRichText = false 禁用丰富文本。

Use isRichText = false to disable rich text.

清晨说晚安 2024-12-07 04:27:30

在您的 NSTextView 中重写此方法:

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

  return [super preferredPasteboardTypeFromArray:availableTypes restrictedToTypesFromArray:allowedTypes];
}

对我来说,这对于粘贴和拖放都有效。

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.

嗳卜坏 2024-12-07 04:27:30

使用以下方法定义自定义 NSTextView 类:

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

注意:从 Mac OS X 开始,粘贴板类型的新 typedef 给出为 NSPasteboardTypeString 而不是 NSStringPBoardType

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

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];
}
反差帅 2024-12-07 04:27:30

上述解决方案确实解决了发问者粘贴文本的问题,但我认为发问者可能想要的不止于此。至少当我来到这里的时候我是这么做的。

我只是希望文本字段中的所有字符始终具有相同的字体,无论它们是通过编程方式插入、从笔尖插入、粘贴、拖入、键入还是由圣诞老人放入克劳斯。我在 Stack Overflow 上搜索了这个更广泛的问题,但没有找到任何问题(或答案)。

而不是此处给出的解决方案使用这个想法。详细来说,给文本字段一个实现这个的委托......

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

完成。这适用于我能想到测试的所有边缘情况。观察到这个选择的变化有点奇怪。似乎观察视图文本对象的字符串值,或者注册 NSTextDidChangeNotification 会更合乎逻辑,但由于我已经设置了委托,并且由于上面的内容已经过测试并得到了 Nick Zitzmann 的赞许,所以我去了与它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文