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