cocoa - NSTextField 还是 NSTextView?

标签 cocoa macos text nstextfield nstextview

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

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

  • 它应该显示大约 1-4 行的文本
  • 它应该能够在文本中显示链接(据我了解,这在两个控件中都应该很简单 - http://developer.apple.com/mac/library/qa/qa2006/qa1487.html)
  • 它应该让用户选择并复制文本
  • 它不应该让用户滚动文本、编辑文本或显示通常出现在可编辑文本字段中的上下文菜单,甚至不应该在此字段中显示文本光标

有了这样的需求,我使用 NSTextField 还是 NSTextView 更好? NSTextField 是否足够好,或者 NSTextView 是否具有 NSTextField 所没有的重要功能?

最佳答案

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...

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

  • it should display text in about 1-4 lines

NSTextView。

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

  • it should let the user select and copy the text

两者都适用。

  • it should NOT let the user scroll the text,

没有 NSScrollView 的 NSTextField 或 NSTextView。您可以在 IB 中执行后者,方法是从库中拖动 TextView ,然后从布局菜单中选择“取消嵌入对象”。

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

两者都适用。

如果您启用选择(通常应该这样做),则当用户单击该字段时,它们会显示一个光标。这是一个功能,因为它指示了用于 Shift-⌘-箭头选择的选择 anchor 的位置。

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

我会使用 NSTextView。

关于cocoa - NSTextField 还是 NSTextView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2398175/

相关文章:

macos - 如果我使用时间机器,为什么还要有版本控制系统?

java - Mac OSX Mavericks (10.9.1) 上 Xerial 的 JDBC SQLite 出现 SQLITE_NOTADB 错误

c++ - Mac - C++ - 创建 file.exe

android - 如何不通过 xml 将文本放在图像按钮上

c# - 二进制搜索算法 : Text File for each record in array

cocoa - 在 Cocoa 中修改大文件

objective-c - 我如何读取 Cocoa 中的环境变量?

Objective-C 保留计数澄清

objective-c - 应用自定义逻辑后,下一个 UI 操作会使我的应用程序崩溃

python,搜索.txt文件并注入(inject)字符