有人可以向我解释一下 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。
- 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)
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/