• 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: NSTableView row height calculation
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSTableView row height calculation


  • Subject: Re: NSTableView row height calculation
  • From: Steven Spencer <email@hidden>
  • Date: Fri, 24 Jun 2005 11:51:44 +0100

Thanks for the pointer.

  - Steve Spencer

On Friday, June 24, 2005, at 01:26AM, Nate Friedman <email@hidden> wrote:

>
><<Original Attached>>
Hi Steven, 

While i was researching a similar problem *LAST WEEK* i came across RowResizableViews. It looks great, especially if you have 1 row that is 3 lines long and the rest are 1 line. The major caveat is that it runs noticeably slower if there is a reasonable sized data source (say 100 rows.) I hope this helps :)

- Nate
On Jun 22, 2005, at 5:16 PM, Steven Spencer wrote:

Hi Karl,

Thanks for your code.
I couldn't get it to return the right heights - the text was clipped.
However, the following code seems to work until the column is resized.
When this happens, the vertical scrollbar doesn't resize.
Perhaps a table reload is required.

- (float)tableView:(NSTableView *)tableView heightOfRow:(int)row
{
  NSString *str = [[myArray objectAtIndex:row] target];
  NSTableColumn *col = [[myTableView tableColumns] objectAtIndex:0];

  NSCell *colDataCell = [[[NSCell alloc] initTextCell:str] autorelease];
  [colDataCell setWraps:YES];
  [colDataCell setFont:[[col dataCell] font]];

  return [colDataCell cellSizeForBounds:NSMakeRect(0.0, 0.0, [col width], FLT_MAX)].height;
}

  - Steve Spencer


On 22 Jun 2005, at 15:49, The Karl Adam wrote:


Or you could try something that takes the table into account as well
as the space necessary for the string to draw, like:

- (float) tableView:(NSTableView *)tableView heightOfRow:(int)row {
   NSString *currentString = [[myArray objectAtIndex:row] target];
   NSTableColumn *columnImInterestedIn = [tableView tableColumns]
objectAtIndex:0];
   NSDictionary *attributes = [[[[columnImInterestedIn dataCell]
attributedStringValue] attributesAtIndex:0 effectiveRange:NULL];

   NSAttributedString *attributedString = [[NSAttributedString
initWithString:currentString attributes:attributes];
   NSSize spaceForString = [attributedString size];

   // We know this is how much space it would like to have, but the
cell probably isn't that wide or tall, so we know that we have to
figure out how many lines it will break into and thus the height we
would like in order to draw this properly

   float rows = ceilf( spaceForString.width / [columnImInterestedIn width] );
   // rows should be how many times the current string would need to
be split to fit in the current width

   return spaceForString.height * rows;
}


Disclaimer: This was written in gmail off the top of my head, just cuz
I think it should work doesn't mean there isn't some horrible bug
waiting to eat your babies in it. And if your computer blows up,
please send me pictures so that I can laugh.

On 6/21/05, Steven Spencer <email@hidden> wrote:


Hello,

When using a single column table view how do I calculate the row
height for multi-line wrapped text in delegate method
tableView:heightOfRow ?

setWraps:YES has been applied to the column dataCell.
I don't know how to account for the cell border.
The code below generates cell heights that are too short and the text
is clipped.

The text height calculation is from :

ADC Home > Reference Library > Documentation > Cocoa > Text & Fonts >
Text Layout > Calculating Text Height.


Thanks.

   - Steve Spencer


Unoptimised code :

- (float)tableView:(NSTableView *)tableView heightOfRow:(int)row
{

   NSString *str = [[myArray objectAtIndex:row] target];
   NSTableColumn *col = [[myTableView tableColumns] objectAtIndex:0];
   NSCell *colDataCell = [col dataCell];
   //float myWidth =  [colDataCell cellSize].width;
   float myWidth = [col width];
   //NSLog(@"cell width=%f", myWidth);

   NSTextStorage *textStorage = [[[NSTextStorage alloc]
initWithString:str] autorelease];
   NSTextContainer *textContainer = [[[NSTextContainer alloc]
initWithContainerSize: NSMakeSize(myWidth, FLT_MAX)] autorelease];
   NSLayoutManager *layoutManager = [[[NSLayoutManager alloc] init]
autorelease];

   [layoutManager addTextContainer:textContainer];
   [textStorage addLayoutManager:layoutManager];

   [textStorage addAttribute:NSFontAttributeName value:[colDataCell
font] range:NSMakeRange(0, [textStorage length])];
   [textContainer setLineFragmentPadding:0.0];

   (void) [layoutManager glyphRangeForTextContainer:textContainer];
   NSRect rct = [layoutManager usedRectForTextContainer:textContainer];

   float height = rct.size.height;
   //float height = [colDataCell drawingRectForBounds:rct].size.height;

   NSLog(@"row %i calculated height=%f", row, height);

   return height;
}
 _______________________________________________




 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >NSTableView row height calculation (From: Steven Spencer <email@hidden>)
 >Re: NSTableView row height calculation (From: The Karl Adam <email@hidden>)
 >Re: NSTableView row height calculation (From: Steven Spencer <email@hidden>)
 >Re: NSTableView row height calculation (From: Nate Friedman <email@hidden>)

  • Prev by Date: CoreData: How to alter attributes undoably?
  • Next by Date: Re: Document can't be deleted after opening and closing it
  • Previous by thread: Re: NSTableView row height calculation
  • Next by thread: Re: NSTableView row height calculation
  • Index(es):
    • Date
    • Thread