iPhone sizewithfont 不起作用?

发布于 2024-09-10 03:26:17 字数 706 浏览 5 评论 0原文

我有以下代码:

- (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

木格 2024-09-17 03:26:17

过一遍这个问题。它描述了 - 您必须使用 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];

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];
無處可尋 2024-09-17 03:26:17

您没有以正确的格式使用 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.

苹果你个爱泡泡 2024-09-17 03:26:17

除了上面提到的有缺陷的类型杂耍之外, 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

﹉夏雨初晴づ 2024-09-17 03:26:17

我花了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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文