Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top