Two core graphics structures, the CGRect and the CGPoint, play a large role in iPhone development. They are used to position items on-screen and to set their size. Every time you use a UIView, you can work with its frame, its center, and its bounds--all of which use these two structures to handle geometry. Here's a quick review of the structures in question.
The CGPoint structure consists of two floats, x and y, providing a location in a 2D coordinate system:
struct CGPoint {
CGFloat x;
CGFloat y;
};
typedef struct CGPoint CGPoint;
Do not confuse the CGPoint structure with the CGSize structure. The latter also contains two floats but these define extents not location:
struct CGSize {
CGFloat width;
CGFloat height;
};
typedef struct CGSize CGSize;
The CGRect structure is built from these two items. It contains a point, which defines the origin of the rectangle and a size, which defines its bounds.
struct CGRect {
CGPoint origin;
CGSize size;
};
typedef struct CGRect CGRect;
With UIViews, you can set their location using setCenter:, which takes a point as its argument or you can define the entire position and size using setFrame:, which takes a rectangle as the argument. The iPhone also offers an unpublished setOrigin: API. I still do not know why Apple has not yet included this as an official API call.
This all having been said, there are a few absolutely lovely and usually overlooked UIKit calls that support these structures and provide easy conversion to and from NString objects. These are functions, not method calls, and are documented in the UIKit Function Reference. These functions are a mishmash of simple utility routines that often get missed in the more usual object overview. As functions, they're called via standard C.
Three functions allow you to convert your CG structs into strings. They are: NSStringFromCGPoint, NSStringFromCGRect, and NSStringFromCGSize. Just pass the structure as an argument. For example:
NSString *myString = NSStringFromCGRect([myView frame]);
This returns a string in the format {{x, y}, {h, w}}, such as {{22.0, 33.0}, {10.0, 20.0}}. The separator is always a comma and the point and size are contained in braces. Once converted, you can log the output, send it to a label, or do whatever else you'd like with the handy string-based format.
You can, of course, go the other way as well. CGPointFromString, CGRectFromString, and CGSizeFromString take you from string back to structure. These functions return the Core Graphics structures that are stored in converted strings. If the string is malformed, the functions return the default Zero structures, i.e. CGPointZero, CGRectZero, or CGSizeZero. Among other reasons to use the functions, converting to strings lets you store these structures as objects using the standard NSDefaults.
There's one more function set that I find insanely useful and want to mention, which is the CGAffineTransform pair. They work just like the structure functions I just went over but they apply to affine transforms instead of sizes, points and rectangles. NSStringFromCGAffineTransform produces a string in the form of {a, b, c, d, tx, ty}. This corresponds to the standard floating point components of the transform. And, as you'd expect, you can extract the transform from this string using CGAffineTransformFromString.

Some of you readers will no doubt have been familiar with these functions but I know that there are many developers out there who may have overlooked these handy utilities. I hope that many of you will have found this little tip useful.
hey, useful post
it has been bookmarked on http://www.iphonekicks.com/objectivec/Converting_Points_and_Rectangles_to_Strings_and_back
cheers
Thank you so much. This post was a major time saver!
This returns a string in the format {{x, y}, {h, w}}, such as {{22.0, 33.0}, {10.0, 20.0}}. The separator uggs sale is always a comma and the point and size are contained in braces. Once converted, you can log the output, send it to a label, or do whatever else you'd like with the handy string-based format.