Re: NSTableView row height calculation
Re: NSTableView row height calculation
- Subject: Re: NSTableView row height calculation
- From: Steven Spencer <email@hidden>
- Date: Thu, 23 Jun 2005 01:16:41 +0100
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