Objective C - 核心文本 在位置查找索引?
我正在尝试获取最接近某个点的字符的索引。
下面的代码工作得很好,因为我的文本对齐方式设置为左,一旦我将其更改为右或居中,它就会给我错误的索引。在获得最接近点的索引时考虑文本对齐的解决方案是什么?
CTLineRef line = (CTLineRef)[lines objectAtIndex:i];
index = CTLineGetStringIndexForPosition(line, point);
编辑:
我的文本对齐是通过添加一个段落样式来完成的,该段落样式的textAlignment设置为kCTCenterTextAlignment
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该点是相对于线的原点(随对齐方式而变化)的,因此您必须使用
CTFrameGetLineOrigins
并将线原点的 x 值添加到点的 x 值中。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.