• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSString sizeWithAttributes inaccuracy
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSString sizeWithAttributes inaccuracy


  • Subject: Re: NSString sizeWithAttributes inaccuracy
  • From: Aki Inoue <email@hidden>
  • Date: Thu, 24 Jul 2008 16:47:55 -0700

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


  • Follow-Ups:
    • Re: NSString sizeWithAttributes inaccuracy
      • From: Jacob Ole Juul Kolding <email@hidden>
References: 
 >NSString sizeWithAttributes inaccuracy (From: Jacob Ole Juul Kolding <email@hidden>)

  • Prev by Date: Re: how to correctly set knob/thumb proportion and scroller orientation in NSScroller
  • Next by Date: Re: NSLog on releasebuild
  • Previous by thread: NSString sizeWithAttributes inaccuracy
  • Next by thread: Re: NSString sizeWithAttributes inaccuracy
  • Index(es):
    • Date
    • Thread