Iphone sizewithfont not working?

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 someone tell me why "[currentMessage sizeWithFont: font forWidth: 270 lineBreakMode: UILineBreakModeWordWrap] .height" always returns zero or zero?

I checked and currentMessage is populated correctly.

Any ideas?

+3
source share
4 answers

Go through this question. He describes this - you need to use, CGFLOAT_MAXor see the following code (captured 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];
+7
source

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

% f float.

0

, , sizeWithFont: forWidth: lineBreakMode: () , .

sizeWithFont: constrainedToSize: lineBreakMode: . CGSizeMake (270.0f, 999999.0f), .

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

0

I spent 2 hours on this, exhausted. For me, the problem was such a small and stupid thing: I had a "release" mode. So, when you go with the debugger, it stops at the correct lines of code (I don’t know why the debugger should do this in release mode), but it did not show what I expected.

0
source

Source: https://habr.com/ru/post/1754748/


All Articles