How do I determine the origin of a CGPath?

625 Views Asked by At

I'm drawing text within a CGPath, in order to do hit-testing on the text, I'm using CTFrameGetLineOrigins. Here's what the documentation says:

Each CGPoint is the origin of the corresponding line in the array of lines returned by CTFrameGetLines, relative to the origin of the frame's path.

How would I go about finding the origin of the frame's path? Examples I've found all save the origin of the path when the path is initially created. I have two problems with this:

  1. The creation of my path is quite distant from the place where I'm doing the hit testing. I would need to make sure that I'm passing around a CGPoint in addition to the CGPath. Ugly, but not insurmountable.
  2. What is the origin for a shape that isn't rectangular? What is the origin of a circular CGPath?
1

There are 1 best solutions below

2
Tony On BEST ANSWER

You can use CGPathApply to examine the full path:

typedef struct {
  CGPoint origin;
  BOOL found;
} MySearchData;

void MyApplierFunction (void* info, const CGPathElement* element) {
  MySearchData* searchData = (MySearchData *) info;
  if (! searchData->found) {
    searchData->origin = element->points[0];
    searchData->found = YES;
  }
}

CGPoint GetPathOrigin (CGPathRef path) {
  MySearchData searchData = { CGPointZero, NO };
  CGPathApply(path, (void *) &searchData, &MyApplierFunction);
  return searchData.origin;
}