1

I am trying to code my first macos app using swift in xcode.

I first use a NSTableView, and add a NSTextField in the tableViewCell. The window is like enter image description here.

What I want to achieve is when typing, the height of the NSTextField changes automatically to show all text in different lines. That means the heights of the tableview cell and NSTextView changes simultaneously.

For example, the height of the NSTextFiled is 5 when you type a line of text; 15 when typing three lines, and 25 for five lines.

In a NSTableView, it seems like the hight of a row is fixed. However, I add a NSTextField in the table cell and want to

david
  • 842
  • 2
  • 8
  • 25
  • I would recommend using a text view instead of a text field and just use a scroll bar instead of sizing the input view. – Lloyd Keijzer May 06 '19 at 15:26
  • 1
    Subsclass your `NSTextField` to achieve auto grow like described here - https://stackoverflow.com/questions/10463680/how-to-let-nstextfield-grow-with-the-text-in-auto-layout Then add autolayout to your `NSTableView`. Look at the proposed solutions here - https://stackoverflow.com/questions/7504546/view-based-nstableview-with-rows-that-have-dynamic-heights – Daniyar May 06 '19 at 15:30
  • @Astoria the answer is exactly what I want, but there is still a problem: how to set the height of the TableViewCell according to the content rather than fixed? I did not see any related setting in Main.storyboard in xcode – david May 06 '19 at 15:50
  • @david take a look at the second popular answer from `NSTableView`-question link above. – Daniyar May 06 '19 at 15:53
  • @Astoria Thanks, that helps a lot! – david May 06 '19 at 16:07

0 Answers0