Setting the size of NSTableView columns

  • I know what text will be appearing in the cells of my table and I am
    building the table dynamically in my code.

    What I would like to be able to do is explicitly set the width of my
    columns to be only as wide as they need to be.

    I believe I can determine the width of the text using the
    sizeWithAttributes message, but I am not certain what the attributes
    should be. Is it possible to determine the attributes which will be
    used for table cells containing text?

    Is there some other way to do what I want?
  • On Dec 10, 2007, at 10:43 PM, Eric Gorr wrote:

    > I know what text will be appearing in the cells of my table and I am
    > building the table dynamically in my code.
    >
    > What I would like to be able to do is explicitly set the width of my
    > columns to be only as wide as they need to be.
    >
    > I believe I can determine the width of the text using the
    > sizeWithAttributes message, but I am not certain what the attributes
    > should be. Is it possible to determine the attributes which will be
    > used for table cells containing text?
    >
    > Is there some other way to do what I want?

    With some more research a solution I found is to do the following:

    NSTextFieldCell *fieldCell          = [tableColumn dataCell];
    NSSize          stringSize;
    NSSize          cellSpacing;
    NSString        *textString        = @"00";
    NSFont          *cellFont          = [fieldCell font];
    NSDictionary    *cellAttributes    = [NSDictionary
    dictionaryWithObject:cellFont

    forKey:NSFontAttributeName];

    stringSize  = [textString sizeWithAttributes:cellAttributes];
    cellSpacing = [cardTable intercellSpacing];

    [tableColumn setWidth:ceil(stringSize.width) + ( cellSpacing.width *
    2 )];

    Is there any reason why this doesn't work?

    I noticed that before I obtain the intercell spacing, the columns
    weren't wide enough. As near as I can tell, this is the way to
    determine how much space surrounds the content of a cell.
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today