NSString sizeWithAttributes inaccuracy
NSString sizeWithAttributes inaccuracy
- Subject: NSString sizeWithAttributes inaccuracy
- From: Jacob Ole Juul Kolding <email@hidden>
- Date: Thu, 24 Jul 2008 23:23:09 +0200
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@hidden
_______________________________________________
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