ios 最大 CGFloat 值是否有常数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6285188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a constant for the maximum CGFloat value?
提问by Proud Member
I need to create a CGSize to compute text height of an arbitrary text with arbitrary length. UIKit has this nice method -sizeWithFont:constrainedToSize: and my text is only constrained in width, but not in height.
我需要创建一个 CGSize 来计算任意长度的任意文本的文本高度。UIKit 有这个不错的方法 -sizeWithFont:constrainedToSize: 并且我的文本仅在宽度上受到限制,而在高度上没有限制。
For this, I need to set the maximum possible CGFloat for the height.
为此,我需要为高度设置最大可能的 CGFloat。
Is there a constant like "CGFloatMax"?
有没有像“CGFloatMax”这样的常数?
采纳答案by highlycaffeinated
回答by GBF_Gabriel
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.
请注意,CGFloat.max
当 Swift 3 出现时,它已被删除,如文档中所述。
回答by jscs
How about CGFLOAT_MAX
?
怎么样CGFLOAT_MAX
?
回答by AamirR
I was looking for a Swift version of minimumCGFloat value and landed here, if you too ;) then here is the answer:
我正在寻找最小CGFloat 值的 Swift 版本并降落在这里,如果你也是;) 那么这里是答案:
CGFloat.leastNormalMagnitude
回答by Paul R
A CGFloat
is just a float
so you can safely use FLT_MAX
from <float.h>
.
ACGFloat
只是 afloat
所以你可以安全地使用FLT_MAX
from <float.h>
。
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个平台上同样的事情。
回答by Leo Thiessen
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 (i.e. CGFLOAT_MAX). As far as I can tell this Apple documentation pageindicates using %f with NSLog should print the double - someone please correct me if this is wrong.
因此,出于某种原因,我的 CGFloat 变量应该是一个双精度值,但是当它被分配一个双精度值(即 CGFLOAT_MAX)时,它似乎是一个溢出的浮点数。据我所知,这个 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,特别是如果您依赖一个专门接受浮点参数而不是双精度或 CGFloat 参数的库。
回答by Rajan Twanabashu
CGSize size = CGSizeMake(CGFLOAT_MIN, CGFLOAT_MAX);
width = 0.0000000000000000000000000000000000000117549435, height = 3.40282347E+38
宽度 = 0.00000000000000000000000000000000000000000000117549435,高度 = 3.40282347E+38
回答by Anson Yao
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 将超出范围。