Is there a constant for the maximum CGFloat value?

2020-05-22 04:34发布

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.

For this, I need to set the maximum possible CGFloat for the height.

Is there a constant like "CGFloatMax"?

8条回答
Melony?
2楼-- · 2020-05-22 04:44
smile是对你的礼貌
3楼-- · 2020-05-22 04:45
CGSize size = CGSizeMake(CGFLOAT_MIN, CGFLOAT_MAX);

width = 0.0000000000000000000000000000000000000117549435, height = 3.40282347E+38

查看更多
smile是对你的礼貌
4楼-- · 2020-05-22 04:54

For those using Swift 2, you should use:

CGFloat.max

For those using Swift 3, you should use:

CGFloat.greatestFiniteMagnitude

Note that CGFloat.max was removed when Swift 3 came out, as reflected in the documentation.

查看更多
太酷不给撩
5楼-- · 2020-05-22 04:55

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.

查看更多
来,给爷笑一个
6楼-- · 2020-05-22 04:56

A CGFloat is just a float so you can safely use 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.

查看更多
ら.Afraid
7楼-- · 2020-05-22 04:58

CGGeometry defines:

#define CGFLOAT_MAX FLT_MAX
查看更多
登录 后发表回答