66

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"?

Proud Member
  • 40,078
  • 47
  • 146
  • 231
  • @answers: upvoted everyone..considering they are within 4 seconds of each other hah. – Jesse Naugher Jun 08 '11 at 21:03
  • It would probably take you less time to check the documentation for a question like this than to post here. – jscs Jun 08 '11 at 21:03
  • 1
    @Josh Caswell: I think a lot of people don't know (at least parts of) the documentation exist. In this case, UIKit uses CGFloat all over, so the questioner might have only looked in the UIKit docs. @Mikhalo Ivanokov, what you need is the Core Graphics (CG) docs: http://developer.apple.com/library/ios/documentation/CoreGraphics/Reference/CoreGraphics_Framework/ Also useful is Apple's online documentation search (which I've found better than Xcode's or, for this purpose, Google): http://developer.apple.com/library/ios/search/?q=CGFloat+max – Peter Hosey Jun 08 '11 at 22:08
  • 1
    @Peter: I know the docs can be hard to navigate, and I understand that it's a skill that has to be learned, but typing "CGFloat" into Xcode's search box takes you straight to the CGGeometry reference. – jscs Jun 08 '11 at 22:12

8 Answers8

191

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.

Sandy Chapman
  • 11,133
  • 3
  • 58
  • 67
GBF_Gabriel
  • 2,636
  • 2
  • 17
  • 15
  • 7
    Note as of Swift 3 this is now `CGFloat.greatestFiniteMagnitude`: "Corresponds to the C macros FLT_MAX, DBL_MAX, etc. The naming of those macros is slightly misleading, because infinity is greater than this value." – jemmons Jul 10 '16 at 14:41
48

CGGeometry defines:

#define CGFLOAT_MAX FLT_MAX
highlycaffeinated
  • 19,729
  • 9
  • 60
  • 91
15

How about CGFLOAT_MAX?

jscs
  • 63,694
  • 13
  • 151
  • 195
12

I was looking for a Swift version of minimum CGFloat value and landed here, if you too ;) then here is the answer:

CGFloat.leastNormalMagnitude
AamirR
  • 11,672
  • 4
  • 59
  • 73
6

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.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • 3
    The type changes depending on system, though. Granted that iOS is all 32-bit right now, but one shouldn't assume the underlying type of a typedef. – jscs Jun 08 '11 at 21:04
  • @Josh: indeed - thanks - I've noted this in a subsequent edit now – Paul R Jun 08 '11 at 21:06
  • CGFloat isn't always a float- when compiling apps on OS X for 64 bit, CGFloat is defined as a double. See CGBase.h, about line 100. – leecbaker Nov 21 '11 at 22:32
  • @leecbaker: it's an iOS-specific question, so everything is 32 bit, for now at least. But yes, you're right, if we expand the discussion to OS X then we have to allow for 64 bit builds where a CGFloat is a double. – Paul R Nov 22 '11 at 07:42
  • @PaulR Oops- I missed the ios tag / UIKit mention when scanning the question. – leecbaker Nov 23 '11 at 03:31
  • 9
    Hey, I just came back from the future and they actually have 64-bit iOS devices there already! ;) – macbirdie Sep 30 '13 at 08:22
1

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 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:

# define CGFLOAT_MAX DBL_MAX

I command+clicked on CGFloat and Xcode took me to another line in the same file:

typedef CGFLOAT_TYPE CGFloat;

Then I command+clicked on the CGFLOAT_TYPE and it jumped to the #define line here:

#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 page indicates using %f with NSLog should print the double - someone please correct me if this is wrong.

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.

1
CGSize size = CGSizeMake(CGFLOAT_MIN, CGFLOAT_MAX);

width = 0.0000000000000000000000000000000000000117549435, height = 3.40282347E+38

Rajan Twanabashu
  • 4,586
  • 5
  • 43
  • 55
0

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.

Anson Yao
  • 1,544
  • 17
  • 27