1

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?
kubi
  • 48,104
  • 19
  • 94
  • 118

1 Answers1

0

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;
}
Tony
  • 3,470
  • 1
  • 18
  • 23
  • It certainly works for rectangular CGPaths. Will this work for other shapes as well? It seems to depend on getting the lower-left-hand path element first.. – kubi Nov 02 '11 at 21:59
  • I don't think you can assume anything about where the first point is relative to the original shape, so you might need to iterate over all the points to find the "corner" you're looking for. – Tony Nov 04 '11 at 19:49