Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here are some extension methods to size columns by their content in C# for Monomac developers. </p> <p><b>NOTE:</b> this sizes columns by their content only, other considerations no considered (empty cols are 10px wide for eg).</p> <p>Thanks to slashlos for the obj-c snippet.</p> <pre><code>public static void SizeColumnsByContent(this NSTableView tableView, int minWidth = 10) { Contract.Requires(tableView != null); Contract.Requires(tableView.DataSource != null); var columns = tableView.TableColumns(); var widths = new List&lt;int&gt;(); for (int row=0; row&lt; tableView.RowCount; row++) { for (int col=0; col &lt; tableView.ColumnCount; col++) { // Determine what the fit width is for this cell var column = columns[col]; var objectValue = tableView.DataSource.GetObjectValue(tableView, column, row); var width = column.DataCell.DetermineFitWidth(objectValue, minWidth); // Record the max width encountered for current coolumn if (row == 0) { widths.Add(width); } else { widths[col] = Math.Max(widths[col], width); } } // Now go resize each column to its minimum data content width for (int col=0; col &lt; tableView.ColumnCount; col++) { columns[col].Width = widths[col]; } } } public static int DetermineFitWidth(this NSCell cell, NSObject objectValueAtCell, int minWidth = 10) { int width; if (cell is NSTextFieldCell) { var font = cell.Font ?? NSFont.ControlContentFontOfSize(-1); var attrs = NSDictionary.FromObjectAndKey(font, NSAttributedString.FontAttributeName); // Determine the text on the cell NSString cellText; if (objectValueAtCell is NSString) { cellText = (NSString)objectValueAtCell; } else if (cell.Formatter != null) { cellText = cell.Formatter.StringFor(objectValueAtCell).ToNSString(); } else { cellText = objectValueAtCell.Description.ToNSString(); } width = (int)cellText.StringSize(attrs).Width + minWidth; } else if (cell.Image != null) { // if cell has an image, use that images width width = (int)Math.Max(minWidth, (int)cell.Image.Size.Width); } else { // cell is something else, just use its width width = (int)Math.Max(minWidth, (int)cell.CellSize.Width); } return width; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload