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

NSTableView row height calculation


  • Subject: NSTableView row height calculation
  • From: Steven Spencer <email@hidden>
  • Date: Wed, 22 Jun 2005 00:17:45 +0100

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


  • Follow-Ups:
    • Re: NSTableView row height calculation
      • From: The Karl Adam <email@hidden>
  • Prev by Date: Re: Function Pointer of Class Member Function in Cocoa
  • Next by Date: Re: NSFileManager - getting list of "safe" files/folders [update]
  • Previous by thread: Re: WWDC 2004 Source Code
  • Next by thread: Re: NSTableView row height calculation
  • Index(es):
    • Date
    • Thread