NSTableView row height calculation
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