sizeWithAttributes 给我一个不同的 CGSize 宽度和高度

sizeWithAttributes gets me a different CGSize width and height

在 iOS 7 中 sizeWithFont: 已弃用。建议的替换方法是sizeWithAttributes:
但是当我将方法从 sizeWithFont: 更改为 sizeWithAttributes:
我得到不同的值。

这是我的代码:

CGSize temp = [opt.option sizeWithFont:[UIFont fontWithName:fontFamily size:[self randomFontSize]]];
NSLog(@"Old SizeWithFont value %f x %f",temp.height, temp.width);

NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:fontFamily size:[self randomFontSize]]};
temp = [opt.option sizeWithAttributes: attributes];
NSLog(@"New SizeWithAttribute has value %f x %f",temp.height, temp.width);

输出为:

linespacing 16.33, fontsize 16.00
Old SizeWithFont value 18.000000 x 47.000000
New SizeWithAttribute has value 17.875000 x 46.250000

我做错了什么吗?我

属性文本方法描述暗示行为差异(我添加的粗体)...

This method returns fractional sizes; to use a returned size to size views, you must raise its value to the nearest higher integer using the ceil function.

它还指出应使用 ceil() 计算整数大小(换句话说,四舍五入)。这样做会使您的实验按预期进行...

NSLog(@"New SizeWithAttribute has value %f x %f",ceil(temp.height), ceil(temp.width));