Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Set column width according to max cell content?

Set column width according to max cell content?
Thread Tools
Brass
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Nov 26, 2003, 08:14 PM
 
How can I set the width of an NSTableColumn according to the size of it's data cells (the cells are all the default NSTextFieldCell objects).

currently I'm caculating the width by retrieving the NSString that is supposed to be displayed in that cell and sending it the "sizeWithAttributes:nil" method, to get the string's size. I do this for all cells in the column, and then use the maximum width returned.

This almost works, but the width of the string, does not appear to be an accurate measure of the width of the column/cell needed to display it.

For small strings, I need to add about 4 to the width. For larger strings, I need to add more.

In any case, even if it was accurate, this seems to be a very kludgy way of sizing a column to fit it's data content. Is there any better way?

(I notice the the "sizeToFit" method for the NSTableColumn, attempts to fit the header content, but there appears to be nothing to do the same for the data content).
     
Tim2 at Omni
Dedicated MacNNer
Join Date: Aug 2001
Location: Seattle, WA
Status: Offline
Reply With Quote
Nov 28, 2003, 03:49 AM
 
Originally posted by Brass:
currently I'm caculating the width by retrieving the NSString that is supposed to be displayed in that cell and sending it the "sizeWithAttributes:nil" method
You're on the right track. Perhaps you need to actually pass in some attributes so that the measured strings use the same font and paragraph style as the strings in the cells?

Refer to NSAttributedString.h in AppKit (not the one in Foundation) for information about how to construct an attributes dictionary.
Tim Omernick
Engineer, The Omni Group
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 01:30 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,