gpt4 book ai didi

ios - NSString sizeWithFont forWidth 返回错误值

转载 作者:行者123 更新时间:2023-12-01 17:26:08 25 4
gpt4 key购买 nike

我有一个奇怪的问题。我的 sizeWithFont: forWidth: lineBreakMode:NSLineBreakByWordWrapping正在返回错误的值。我有一个字符串数组,需要放在一个整洁的“表”中。单元格基本上是 UIViewUILabel s 在其中。为了分配初始化单元格 View 和带有正确框架的标签,我需要预先计算单元格的所需高度和包装 View 的总高度,因为所有单元格都将放置在另一个 View 中。我的代码如下所示:

#define kStandardFontOfSize(x)  [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:x]

CGFloat size = 0.0f; //for computing the total size as cells will be placed in another view
items = [NSArray arrayWithObjects:@"You have 23 new followers", @"1125 new likes", @"Successful week with 24 new Twitter followers and 60 new email subscribers", @"1125 new tickets", nil];

for (NSString *item in items)
{
if ([item sizeWithFont:kStandardFontOfSize(16) forWidth:100 lineBreakMode:NSLineBreakByWordWrapping].height < 25)
size += 70; //either the cell will be 70 (140) pixels tall or 105 (210)pixels
else
size += 105;
NSLog(@"%f, %f, %@", [item sizeWithFont:kStandardFontOfSize(16) forWidth:100 lineBreakMode:NSLineBreakByWordWrapping].width, [item sizeWithFont:kStandardFontOfSize(16) forWidth:100 lineBreakMode:NSLineBreakByWordWrapping].height, item);
}

但是日志返回了非常奇怪的值:
82.000000, 20.000000, You have 23 new followers
99.000000, 20.000000, 1125 new likes
70.000000, 20.000000, Successful week with 24 new Twitter followers and 60 new email subscribers
67.000000, 20.000000, 1125 new tickets

“1125个新点赞”的宽度怎么可能是99,而长串只有70呢?高度肯定应该大于20还是?

最佳答案

尝试使用 [[NSString string] sizeWithFont:UIFont constrainedToSize:CGSize lineBreakMode:(NSLineBreakMode)]

关于ios - NSString sizeWithFont forWidth 返回错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18254729/

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