macos NSTextField 还是 NSTextView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2398175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:48:40  来源:igfitidea点击:

NSTextField or NSTextView?

cocoamacostextnstextfieldnstextview

提问by Kuba Suder

Could someone explain to me what are the main differences between NSTextField and NSTextView? I know that NSTextView has more features and is usually used for longer texts, and NSTextField is usually used for one-line plain text fields, but if I understand correctly, NSTextField can be also used with attributed strings and with multiple lines...

有人可以向我解释 NSTextField 和 NSTextView 之间的主要区别是什么吗?我知道 NSTextView 有更多功能,通常用于较长的文本,而 NSTextField 通常用于单行纯文本字段,但如果我理解正确,NSTextField 也可以用于属性字符串和多行...

What I need specifically is a control that would display the text of messages inside a timeline view like in Tweetie or any other similar software. The only requirements I have are:

我特别需要的是一个控件,它可以在时间线视图中显示消息文本,如 Tweetie 或任何其他类似软件。我的唯一要求是:

  • it should display text in about 1-4 lines
  • it should be able to show links in the text (as I understand, this should be simple in both controls - http://developer.apple.com/mac/library/qa/qa2006/qa1487.html)
  • it should let the user select and copy the text
  • it should NOT let the user scroll the text, edit the text, or show the context menu that usually appears in editable text fields, it shouldn't even show a text cursor in this field
  • 它应该在大约 1-4 行中显示文本
  • 它应该能够在文本中显示链接(据我了解,这在两个控件中都应该很简单 - http://developer.apple.com/mac/library/qa/qa2006/qa1487.html
  • 它应该让用户选择和复制文本
  • 它不应该让用户滚动文本、编辑文本或显示通常出现在可编辑文本字段中的上下文菜单,它甚至不应该在该字段中显示文本光标

With such requirements, is it better for me to use a NSTextField or NSTextView? Is NSTextField good enough, or does NSTextView has something important that NSTextField doesn't?

有这样的要求,我使用 NSTextField 还是 NSTextView 更好?NSTextField 足够好,还是 NSTextView 有一些重要的东西,而 NSTextField 没有?

回答by Peter Hosey

Could someone explain to me what are the main differences between NSTextField and NSTextView? I know that NSTextView has more features and is usually used for longer texts, and NSTextField is usually used for one-line plain text fields, but if I understand correctly, NSTextField can be also used with attributed strings and with multiple lines...

有人可以向我解释 NSTextField 和 NSTextView 之间的主要区别是什么吗?我知道 NSTextView 有更多功能,通常用于较长的文本,而 NSTextField 通常用于单行纯文本字段,但如果我理解正确,NSTextField 也可以用于属性字符串和多行...

Technically true, but you normally use text fields for values that are plain text and usually only one line. (Handle multiple lines, as a text field can accept them. If nothing else, strip the line breaks in a way that makes sense for what you're doing with the text.)

从技术上讲是正确的,但您通常将文本字段用于纯文本且通常只有一行的值。(处理多行,因为文本字段可以接受它们。如果没有别的,请以对您正在处理的文本有意义的方式去除换行符。)

  • it should display text in about 1-4 lines
  • 它应该在大约 1-4 行中显示文本

NSTextView.

NSTextView。

NSTextView. Supporting links in an NSTextField is tricky.

NSTextView。在 NSTextField 中支持链接很棘手。

  • it should let the user select and copy the text
  • 它应该让用户选择和复制文本

Either will work for this.

要么会为此工作。

  • it should NOT let the user scroll the text,
  • 它不应该让用户滚动文本,

NSTextField or NSTextView without NSScrollView. You can do the latter in IB by dragging a text view from the Library and then choosing “Unembed Objects” from the Layout menu.

没有 NSScrollView 的 NSTextField 或 NSTextView。您可以在 IB 中通过从库中拖动文本视图然后从布局菜单中选择“Unembed Objects”来完成后者。

edit the text,

编辑文本,

Either will work for this.

要么会为此工作。

or show the context menu that usually appears in editable text fields,

或显示通常出现在可编辑文本字段中的上下文菜单,

Yes, it should. You should always offer menu items like “Copy” and read-only services. Either control should do this for you; don't fight this.

是的,应该。您应该始终提供“复制”和只读服务等菜单项。任何一个控件都应该为您执行此操作;不要打这个。

it shouldn't even show a text cursor in this field

它甚至不应该在该字段中显示文本光标

Either will work for this.

要么会为此工作。

If you leave selection enabled (which you generally should), they'll show a cursor if the user clicks in the field. This is a feature, as it indicates where the selection anchor is for shift-?-arrow selection.

如果您启用选择(通常应该这样做),如果用户单击该字段,它们将显示一个光标。这是一个功能,因为它指示选择锚点用于 shift-?-arrow 选择的位置。

With such requirements, is it better for me to use a NSTextField or NSTextView?

有这样的要求,我使用 NSTextField 还是 NSTextView 更好?

I would use NSTextView.

我会使用 NSTextView。