0

我正在尝试根据内容将是什么来计算表格视图中单元格的必要高度。这是在- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath.

但是,有时 sizeWithFont 返回 0。我不知道这是怎么可能的,因为除了一个之外,所有参数都是硬编码的,而且我可以看到该参数没有改变。

NSString *address = [properties objectAtIndex:1];

CGFloat calculatedSize = [address sizeWithFont:[[UIFont alloc] fontWithSize:15.f] constrainedToSize:CGSizeMake(207.f, 100.f) lineBreakMode:UILineBreakModeWordWrap].height;

NSLog(@"Calculated size for %@: %f", address, calculatedSize);

calculatedSize += 19.f;

return calculatedSize;

如果我通过多次来回查看视图来尝试几次,这就是输出:

Calculated size for 1234 Fake St: 0.000000
Calculated size for 1234 Fake St: 0.000000
Calculated size for 1234 Fake St: 0.000000
Calculated size for 1234 Fake St: 38.000000
Calculated size for 1234 Fake St: 0.000000
Calculated size for 1234 Fake St: 38.000000
4

2 回答 2

3

代替:

[[UIFont alloc] fontWithSize:15.0f]

和:

[UIFont systemFontOfSize:15.f]
于 2012-06-25T09:03:18.947 回答
0

我测试了这个并且它有效

NSString *address = @"Sample text Sample text Sample text Sample text Sample text Sample text Sample text";

CGFloat calculatedSize = [address sizeWithFont:[UIFont systemFontOfSize:15.0] 
                             constrainedToSize:CGSizeMake(207.f, 100.f) 
                                 lineBreakMode:UILineBreakModeWordWrap].height;    

NSLog(@"Calculated size for %@: %f", address, calculatedSize); //String... 76.000000
于 2012-06-25T09:06:04.833 回答