简体   繁体   English

Iphone sizewithfont不起作用?

[英]Iphone sizewithfont doesn't work?

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? 任何人都可以告诉我为什么“[currentMessage sizeWithFont:font forWidth:270 lineBreakMode:UILineBreakModeWordWrap] .height”总是返回null或nil?

I've checked and currentMessage is populated correctly. 我检查过并正确填充了currentMessage。

Any ideas? 有任何想法吗?

Go through this question. 回答这个问题。 It describes that - you must have to use, CGFLOAT_MAX or see following code ( grabbed from there . ) 它描述了 - 您必须使用CGFLOAT_MAX或查看以下代码(从那里抓取)。

 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];

You are not using NSLog() in correct format. 您没有以正确的格式使用NSLog()。 it should be NSLog(@" %@",[NSString stringWithFormat:@"Height: %f",[currentMessage sizeWithFont:font forWidth:270 lineBreakMode:UILineBreakModeWordWrap].height]); 它应该是NSLog(@“%@”,[NSString stringWithFormat:@“Height:%f”,[currentMessage sizeWithFont:font forWidth:270 lineBreakMode:UILineBreakModeWordWrap] .height]);

and %f should be used for float. 和%f应该用于浮动。

Apart from your flawed type juggling which has been noted above, sizeWithFont:forWidth:lineBreakMode: only measures dimensions of the (truncated) first line, oddly enough. 除了上面提到的有缺陷的类型杂耍之外,sizeWithFont:forWidth:lineBreakMode:仅测量(截断的)第一行的尺寸,奇怪的是。

you want to use sizeWithFont:constrainedToSize:lineBreakMode: which actually splits text over lines and takes the lines into account. 你想使用sizeWithFont:constrainedToSize:lineBreakMode:它实际上将文本分成线并将线条考虑在内。 Use a CGSizeMake(270.0f,999999.0f) to get the full height of the text. 使用CGSizeMake(270.0f,999999.0f)获取文本的完整高度。

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

I spent 2 hours on this, freaking out. 我在这上花了2个小时,吓坏了。 For me the problem was in such a small and stupid thing: I had a 'release' mode switched on. 对我来说问题出在这么小而愚蠢的事情上:我打开了'释放'模式。 So, when going with debugger, it was stopping at the proper code lines (no idea why debugger should do that in release mode), but didn't show what I expected. 因此,当使用调试器时,它停在适当的代码行(不知道为什么调试器应该在发布模式下这样做),但没有显示我的预期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM