Re: Auto-sized table cells, for macOS
Re: Auto-sized table cells, for macOS
- Subject: Re: Auto-sized table cells, for macOS
- From: Quincey Morris <email@hidden>
- Date: Fri, 17 Mar 2017 11:24:39 -0700
On Mar 17, 2017, at 01:45 , Daryle Walker <email@hidden> wrote:
>
> Besides those who intentionally want truncating behavior, wouldn’t “expand to show all text” be the expected default? The current default isn’t friendly to end users.
I’m a bit puzzled about what you’re expecting. You asked about determining row heights in a table. In that case, the current default is to use fixed row heights, and that is the expected default for table rows, regardless of the kind of content in them.
So, you’re talking about text in isolation, which is a different issue.
> Unfortunately, you didn’t provide your Stack Overflow sources.
Because it doesn’t provide a clean working solution, so I was trying to save you the work of reinventing that particular wheel.
> I made my first attempt after reading <http://stackoverflow.com/a/32332743/1010226 <http://stackoverflow.com/a/32332743/1010226>>:
>
>> // Increase the row height to fit all the text (instead of the first line).
>> func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
>> precondition(tableView === self.headerTableView)
>>
>> var height = tableView.rowHeight
>> let nameColumn = tableView.tableColumn(withIdentifier: Names.nameColumnIdentifier)!
>> let bodyColumn = tableView.tableColumn(withIdentifier: Names.bodyColumnIdentifier)!
>> let field = (self.headerArrayController.arrangedObjects as! NSArray).object(at: row) as! RawHeaderField
>> let attributes = [NSFontAttributeName: NSFont.systemFont(ofSize: 0)]
>> let nameString = NSAttributedString(string: field.name, attributes: attributes)
>> let bodyString = NSAttributedString(string: field.body, attributes: attributes)
>> for (string, column) in [(nameString, nameColumn), (bodyString, bodyColumn)] {
>> let frame = NSRect(x: 0.0, y: 0.0, width: column.width, height: .greatestFiniteMagnitude)
>> let view = NSTextView(frame: frame)
>> view.textStorage?.setAttributedString(string)
>> view.isHorizontallyResizable = false
>> view.sizeToFit()
>> height = max(height, view.frame.size.height /*+ 20*/)
>> }
>>
>> return height
>> }
This isn’t what you want, for two entirely separate reasons.
1. Assuming you’re using view-based table views (and if you’re not, you really should be), then you don’t want the height of the text, you want the height of the cell (NSTableCellView) that contains your text in a subview. The whole point is that you leverage autolayout to get the NSTableCellView to compute the text height for you. Furthermore, the text is placed *within* the cell, so the height of the NSTableCellView may be bigger than the height of the text, or (in general) the cell might contain other views that need to be taken into account.
2. You shouldn’t be using a NSTextView, but a NSTextField. A text view typically is placed within a fixed height frame, wrapped in a scroll view, so the concept of an intrinsic height is problematic. What is your actual cell structure, text view or text field?
> But it only establishes the row heights once, at start but never after any resizes (column or whole-table).
As I said before, you have to watch for column width changes and recalculate your heights accordingly. There is no automatic way of handling variable heights on the Mac that matches the way it can be done on iOS.
> It still is short on some long lines. (Is it because I use word-wrap, instead of by character, for that column?) Also, I hard-coded the font to match what the table has. Is there any way to read what the cell template has? (“NSTableView.view(something)” can generate a view for a given row and column, but Apple specifically barred it (with an assert/exception) during this method.)
Using autolayout on the cell as a whole will avoid all of these issues for the text in particular. You’ll get the metrics as implied by the way your text field is configured.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden