Try using -boundingRectWithSize:options:attributes instead with NSStringDrawingUsesDeviceMetics.

Aki

On 2008/07/24, at 14:23, Jacob Ole Juul Kolding wrote:

Hello List

I'm working on an app where I have a NSTableView with one column containing strings. I want this column to automatically resize itself within the ScrollView to fit the width of the widest string.

In order to do this I came up with the following code:

-(void)calculateCellTextAttribs{
        
NSFont* font = [[[[myTableView tableColumns] objectAtIndex:0] dataCell] font];
        myCellAttributes = [[NSMutableDictionary alloc] init];
        [myCellAttributes setObject:font forKey:NSFontAttributeName];
}

-(float)stringPixelWidth:(NSString*) theString{
        NSSize extent = [theString sizeWithAttributes:myCellAttributes];
        return 1.1*extent.width;
}

-(void)updateTable{
        [myTableView reloadData];

        if([records count]){ //records is a NSArray of NSString
                                
                float maxWidth = 0;
                int i = 0;
                for(i = 0; i < [records count]; i++){
                        float tiw = [self stringPixelWidth:[records 
objectAtIndex:i]];
                        if(tiw > maxWidth){
                                maxWidth = tiw;
                        }
                }
                
                [[[myTableView tableColumns] objectAtIndex:0] 
setWidth:maxWidth];
        }
}

The problem is that on very large strings sizeWithAttributes comes up short [no pun intended]
Hence the 1.1 return multiplier in stringPixelWidth.

Does anyone know why this happens or if there is a simpler way to achieve my goal?

Any help appreciated!

Jacob Kolding
[EMAIL PROTECTED]





_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/aki%40apple.com

This email sent to [EMAIL PROTECTED]

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to