简体   繁体   English

最大 CGFloat 值是否有常数?

[英]Is there a constant for the maximum CGFloat value?

I need to create a CGSize to compute text height of an arbitrary text with arbitrary length.我需要创建一个 CGSize 来计算任意长度的任意文本的文本高度。 UIKit has this nice method -sizeWithFont:constrainedToSize: and my text is only constrained in width, but not in height. UIKit 有这个很好的方法 -sizeWithFont:constrainedToSize: 我的文本只在宽度上受到限制,但在高度上没有。

For this, I need to set the maximum possible CGFloat for the height.为此,我需要为高度设置最大可能的 CGFloat。

Is there a constant like "CGFloatMax"?有没有像“CGFloatMax”这样的常数?

For those using Swift 2, you should use:对于使用 Swift 2 的用户,您应该使用:

CGFloat.max

For those using Swift 3, you should use:对于使用 Swift 3 的用户,您应该使用:

CGFloat.greatestFiniteMagnitude

Note that CGFloat.max was removed when Swift 3 came out, as reflected in the documentation .请注意,当 Swift 3 出现时, CGFloat.max已被删除,如文档中所述。

CGGeometry defines: CGGeometry定义:

#define CGFLOAT_MAX FLT_MAX

How about CGFLOAT_MAX ? CGFLOAT_MAX怎么样?

A CGFloat is just a float so you can safely use FLT_MAX from <float.h> . CGFloat只是一个float ,因此您可以安全地使用<float.h>中的FLT_MAX

EDIT: As others have now pointed out it looks like CGFLOAT_MAX is already defined for you so you should use that for consistency rather than FLT_MAX , even though they are the same thing on 32 bit platforms.编辑:正如其他人现在指出的那样,看起来CGFLOAT_MAX已经为您定义了,因此您应该使用它来保持一致性而不是FLT_MAX ,即使它们在 32 位平台上是相同的。

I was looking for a Swift version of minimum CGFloat value and landed here, if you too;) then here is the answer:我一直在寻找最小CGFloat 值的 Swift 版本,如果你也是的话,我会在这里登陆;)那么这里就是答案:

CGFloat.leastNormalMagnitude

This is more of a comment than an answer however I don't have enough reputation to comment at the present.这更像是评论而不是答案,但是我目前没有足够的声誉发表评论。

I just came across unexpected behavior with using CGFLOAT_MAX: on an iPhone 5S (64bit) device running iOS 7.1.2 and using Xcode 5.1.1, the following code:我刚刚遇到了使用 CGFLOAT_MAX 的意外行为:在运行 iOS 7.1.2 并使用 Xcode 5.1.1 的 iPhone 5S(64 位)设备上,以下代码:

CGFloat numRot = floorf(CGFLOAT_MAX / 360.0);
NSLog(@"numRot = %.2f", numRot);

Outputs:输出:

numRot = inf

Whereas, this code:而这段代码:

CGFloat numRot = floorf(FLT_MAX / 360.0);
NSLog(@"numRot = %.2f", numRot);

Correctly outputs:正确输出:

numRot: 945228740662580166143622731901435904.00

I command+clicked on the CGFLOAT_MAX and Xcode took me to the CoreGraphics frameworks CGBase.h file with the following macro definition:我命令+单击 CGFLOAT_MAX 和 Xcode 将我带到具有以下宏定义的 CoreGraphics 框架 CGBase.h 文件:

# define CGFLOAT_MAX DBL_MAX

I command+clicked on CGFloat and Xcode took me to another line in the same file:我命令+单击 CGFloat 和 Xcode 将我带到同一文件中的另一行:

typedef CGFLOAT_TYPE CGFloat;

Then I command+clicked on the CGFLOAT_TYPE and it jumped to the #define line here:然后我命令+单击 CGFLOAT_TYPE 并跳转到 #define 行:

#if defined(__LP64__) && __LP64__
# define CGFLOAT_TYPE double
...

So, for some reason my CGFloat variable is supposed to be a double, however it appears to be a float that overflows when it gets assigned a double value (ie CGFLOAT_MAX).因此,出于某种原因,我的 CGFloat 变量应该是一个双精度变量,但是它似乎是一个浮点数,当它被分配一个双精度值(即 CGFLOAT_MAX)时会溢出。 As far as I can tell this Apple documentation page indicates using %f with NSLog should print the double - someone please correct me if this is wrong.据我所知, 这个 Apple 文档页面表明使用 %f 和 NSLog 应该打印双精度 - 如果这是错误的,请有人纠正我。

All that to say, if FLT_MAX works for your case, you may want to stick with that for now instead of CGFLOAT_MAX, especially if you are relying on a library that specifically accept float arguments instead of double or CGFloat.综上所述,如果 FLT_MAX 适用于您的情况,您现在可能要坚持使用它而不是 CGFLOAT_MAX,特别是如果您依赖一个专门接受浮点 arguments 而不是双精度或 CGFloat 的库。

CGSize size = CGSizeMake(CGFLOAT_MIN, CGFLOAT_MAX);

width = 0.0000000000000000000000000000000000000117549435, height = 3.40282347E+38宽度 = 0.0000000000000000000000000000000000000117549435,高度 = 3.40282347E+38

In Swift 3.0, You can also use CGFloat(FLT_MAX), especially if you want to use it in other cases like zIndex, where CGFloat.greatestFiniteMagnitude will be out of range.在 Swift 3.0 中,您还可以使用 CGFloat(FLT_MAX),特别是如果您想在 zIndex 等其他情况下使用它,其中 CGFloat.greatestFiniteMagnitude 将超出范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM