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. 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”?
- Binary operator '+' cannot be applied to two CGFloat operands?
- “Int is not convertible to CGFloat” error when subclassing UIImageView
- Changing an Int to a CGFloat in Swift to return heightForRowAtIndexPath tableview function
- Conversion between CGFloat and NSNumber without unnecessary promotion to Double
- Converting CGFloat to String in Swift
- CGFloat addition bug?
8 Solutions Collect From Internet About “Is there a constant for the maximum CGFloat value?”
#define CGFLOAT_MAX FLT_MAX
For those using Swift 2, you should use:
For those using Swift 3, you should use:
CGFloat.max was removed when Swift 3 came out, as reflected in the documentation.
CGFloat is just a
float so you can safely use
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.
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);
numRot = inf
Whereas, this code:
CGFloat numRot = floorf(FLT_MAX / 360.0); NSLog(@"numRot = %.2f", numRot);
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.
CGSize size = CGSizeMake(CGFLOAT_MIN, CGFLOAT_MAX);
width = 0.0000000000000000000000000000000000000117549435, height = 3.40282347E+38
I was looking for a Swift version of minimum CGFloat value and landed here, if you too 😉 then here is the answer:
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.
- While mail sending process using ios 8 simulators generates error
- Coredata / SWIFT get records with relationship
- How to use background thread in swift?
- Exit Code 1 Error after building iOS Application
- Erasing part of an image that overlaps with text from a UILabel
- How to Make a Secret iOS App Text Animation
- Function inside prepareForSegue will not execute
- iOS Detection of Screenshot?
- Sorting array alphabetically with number
- ObjectMapper how to map different object based on JSON
- UITabBarController with diffent storyboard file for each ViewController of the TabBar
- Opening a PDF document when clicking a button
- Good practice to use git together with svn
- NSFetchedResultsControllerDelegate animating deletion on wrong indexPath
- UINavigationController-Alike for Desktop-Cocoa?