Re: NSString sizeWithAttributes inaccuracy
Re: NSString sizeWithAttributes inaccuracy
- Subject: Re: NSString sizeWithAttributes inaccuracy
- From: Jacob Ole Juul Kolding <email@hidden>
- Date: Fri, 25 Jul 2008 11:49:51 +0200
- Resent-date: Fri, 25 Jul 2008 11:53:08 +0200
- Resent-from: Jacob Ole Juul Kolding <email@hidden>
- Resent-message-id: <email@hidden>
- Resent-to: email@hidden
IIRC from my debugging sessions sizeWithAttributes already does that?
Jacob Kolding
email@hidden
On Jul 25, 2008, at 1:47 AM, Aki Inoue wrote:
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@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
_______________________________________________
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