我试图根据内容将要为tableview中的单元格计算必要的高度。这在- (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

最佳答案

更换:

[[UIFont alloc] fontWithSize:15.0f]


与:

[UIFont systemFontOfSize:15.f]

关于ios - sizeWithFont返回随机值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11186197/

10-09 01:26