我正在尝试根据内容的内容来计算表格视图中单元格的必要高度。这是在- (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发布于 2012-06-25 17:03:19
替换:
[[UIFont alloc] fontWithSize:15.0f]通过以下方式:
[UIFont systemFontOfSize:15.f]发布于 2012-06-25 17:06:05
我测试了一下,它起作用了。
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.000000https://stackoverflow.com/questions/11186197
复制相似问题