views:

79

answers:

3

Hi Guys, Im going crazy here. Need some help. I have the following code:

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *currentMessage = [FeedMessages objectAtIndex:indexPath.row];

NSLog(currentMessage);

UIFont *font = [UIFont systemFontOfSize:14];

NSLog([NSString stringWithFormat:@"Height: %@",[currentMessage sizeWithFont:font forWidth:270 lineBreakMode:UILineBreakModeWordWrap].height]);

return [currentMessage sizeWithFont:font forWidth:270 lineBreakMode:UILineBreakModeWordWrap].height;

}

Can anybody tell me why "[currentMessage sizeWithFont:font forWidth:270 lineBreakMode:UILineBreakModeWordWrap].height" is always returning null or nil?

Ive checked and currentMessage is populated correctly.

Any ideas?

Thanks in advance

A: 

You are not using NSLog() in correct format. it should be NSLog(@" %@",[NSString stringWithFormat:@"Height: %f",[currentMessage sizeWithFont:font forWidth:270 lineBreakMode:UILineBreakModeWordWrap].height]);

and %f should be used for float.

Ideveloper
A: 

Apart from your flawed type juggling which has been noted above, sizeWithFont:forWidth:lineBreakMode: only measures dimensions of the (truncated) first line, oddly enough.

you want to use sizeWithFont:constrainedToSize:lineBreakMode: which actually splits text over lines and takes the lines into account. Use a CGSizeMake(270.0f,999999.0f) to get the full height of the text.

see http://developer.apple.com/iphone/library/documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html

mvds
A: 

Go through this question. It describes that - you must have to use, CGFLOAT_MAX or see following code ( grabbed from there . )

 NSString *text = @"A really long string in here"; 
 CGSize theSize = [text sizeWithFont:[UIFont boldSystemFontOfSize:18.0f] constrainedToSize:CGSizeMake(265.0f, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
 NSString *stringHeight = [NSString stringWithFormat:@"%f", theSize.height];
sugar