gpt4 book ai didi

ios - sizeWithFont 返回随机值

转载 作者:行者123 更新时间:2023-11-28 20:34:03 25 4
gpt4 key购买 nike

我正在尝试根据内容的内容计算表格 View 中单元格的必要高度。这是在 - (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;

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com