gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:54:24 26 4
gpt4 key购买 nike

在 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));

关于ios - sizeWithAttributes 给我一个不同的 CGSize 宽度和高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54131640/

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