iPhone sizewithfont 不起作用?
我有以下代码:
- (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;
}
谁能告诉我为什么“[currentMessage sizeWithFont:font forWidth:270 lineBreakMode:UILineBreakModeWordWrap].height”总是返回 null 或 nil?
我已经检查过,currentMessage 已正确填充。
有什么想法吗?
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?
I've checked and currentMessage is populated correctly.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
过一遍这个问题。它描述了 - 您必须使用
CGFLOAT_MAX
或查看以下代码(从那里抓取。)Go through this question. It describes that - you must have to use,
CGFLOAT_MAX
or see following code ( grabbed from there . )您没有以正确的格式使用 NSLog() 。应该是
NSLog(@" %@",[NSString stringWithFormat:@"高度: %f",[currentMessage sizeWithFont:font forWidth:270 lineBreakMode:UILineBreakModeWordWrap].height]);
%f 应该用于浮动。
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.
除了上面提到的有缺陷的类型杂耍之外, 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
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
我花了2个小时在这上面,吓坏了。对我来说,问题出在这样一个小而愚蠢的事情上:我打开了“发布”模式。因此,当使用调试器时,它停在正确的代码行处(不知道为什么调试器应该在发布模式下执行此操作),但没有显示我所期望的内容。
I spent 2 hours on this, freaking out. 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.