5

I'm building a chat application with the chat pane as an NSTableView and each message held within a custom cell in a row in that table. I need to make the table row height relative to the message size. After many frustrations with trying to calculate the size of the wrapping text within an NSTextField (holding the message), and then finding out that NSTextField is notoriously bad for this (see here, which is corroborated by my findings. Furthermore, NSTextField resizes completely randomly on occasion (a single message with "?" will resize to be about 5 lines high...) which I'm hoping NSTextView will avoid), I've decided to use an NSTextView instead.

But I can't add an NSTextView to my application within it being embedded in an NSScrollView. I certainly do not want my message boxes to scroll -- I want them to grow depending on the size of their contents. But I can't drag the TextView out of the ScrollView. I tried adding a custom view and setting it to be a TextView, but although I hooked it up to my ViewController in precisely the way I had done my TextField, it didn't display anything.

SuddenMoustache
  • 883
  • 7
  • 17
  • Create your table cells and NSTextView programmatically. – rocky Sep 30 '16 at 21:23
  • That's certainly an alternate approach, but is that really the only solution? Abandon Interface Builder? – SuddenMoustache Oct 02 '16 at 17:56
  • OK. Let's go with your custom view path. What do you mean when you say 'it didn't display anything' ? You should at least see a white box where you placed the custom view in IB, even if you didn't hook it up to your ViewController. – rocky Oct 08 '16 at 04:43
  • I'm running into this same problem. Did you ever figure this out? – Clifton Labrum Feb 06 '19 at 19:57
  • I didn't. In the end I opted to just replace the message view with an HTML view using WKWebView. HTML is much better at resizing dynamic elements than anything Apple has to offer. If you're committed to NSTextView, you could just constrain the height of the text view to be the content height, such that it doesn't scroll. But you'd still be paying for the overhead of having a scroll view that you're not using. – SuddenMoustache Feb 14 '19 at 21:40
  • 1
    It is possible to do this, but with limited Attribute editing for the NSTextView itself (i.e. no setting font etc). See my answer here: https://stackoverflow.com/a/56854375/978300 – Giles Jul 02 '19 at 14:13

0 Answers0