NSTextField or NSTextView?

2019-03-09 05:40发布

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

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:

  • 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

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?

1条回答
三岁会撩人
2楼-- · 2019-03-09 06:32

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

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

NSTextView.

NSTextView. Supporting links in an NSTextField is tricky.

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

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.

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

I would use NSTextView.

查看更多
登录 后发表回答