开发者

Objective C - Core Text Find index at position?

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜