Objective C - Core Text Find index at position?

1.3k views Asked by At

I am trying to get the index of the closest character to a point.

The code below works perfectly fine as ling as my text alignment is set to left, as soon as I chage it to right or center it gives me the wrong index. What would be the solution to consider the text alignment while getting closest index to a point?

CTLineRef line = (CTLineRef)[lines objectAtIndex:i];
index = CTLineGetStringIndexForPosition(line, point);  

EDIT:

My text alignment is done by adding a paragraph style that has it's textAlignment setting to kCTCenterTextAlignment

1

There are 1 answers

2
omz On BEST ANSWER

The point is relative to the line's origin (which changes with the alignment), so you have to use CTFrameGetLineOrigins and add the x value of the line's origin to your point's x value.