简体   繁体   English

Objective C - 核心文本在位置查找索引?

[英]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 我的文本对齐是通过添加一个段落样式来完成的,该样式的textAlignment设置为kCTCenterTextAlignment

该点相对于线的原点(随着对齐而变化),因此您必须使用CTFrameGetLineOrigins并将线的原点的x值添加到点的x值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM