在UITextView中获取单词的X和Y坐标 [英] Get X and Y coordinates of a word in UITextView

查看:252
本文介绍了在UITextView中获取单词的X和Y坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了一个开发人员(Codea的创建者TwoLivesLeft)他们如何在他们的应用程序中进行语法突出显示。他回答说:

I asked a developer (TwoLivesLeft, the creators of Codea) how they did syntax highlighting in their app. He replied :


@ TD2 Codea编辑器是使用UITextView实现的。
突出显示是通过在适当的
位置(通常是UILabels)中叠加子视图来完成的。它们从重用池中出列,
类似于UITableViewCells的工作方式。在滚动期间,
行需要重新突出显示池中的拉出标记以及
移出屏幕的行将其标记转储回池中。

@TD2 the Codea editor is implemented using a UITextView. The highlighting is done by overlaying subviews in the appropriate positions — usually UILabels. They are dequeued from a re-use pool, similar to the way UITableViewCells work. During scrolling, the lines requiring re-highlighting pull markers out of the pool and lines that have moved off screen dump their markers back into the pool.

任何人都可以解释我如何获得某个单词的x和y吗?

Can anyone explain how I would get the x and y of a certain word?

推荐答案

UITextView符合UITextInput,其中可以找到详细说明这里

UITextView conforms to UITextInput, of which a detailed description can be found here.

查看所需的方法textRangeFromPosition:toPosition:, positionFromPosition:offset:,positionFromPosition:inDirection:offset:,以及UITextInput协议中的一些其他基于几何的方法。那些可能会提供您正在寻找的功能。

Take a look at the required methods "textRangeFromPosition:toPosition:", "positionFromPosition:offset:", "positionFromPosition:inDirection:offset:", and some of the other geometric-based methods in the UITextInput Protocol. Those might provide the functionality you are looking for.

我实际上并没有尝试确保这些工作方式也是您想要的,但这看起来就像是关于你的需要。

I have not actually tried to make sure these work the way you want them too, but that looks like its about what you need.

如果您需要更多帮助,请告诉我们!

Let me know if you need any more help!

更新:

以下是一些如何执行此操作的示例代码。我最终得到了firstRectForRange:方法。此代码基本上采用UITextViewtextStuff的最后三个字母并将其突出显示为绿色。

Here is some sample code of how to do this. I ended up getting the "firstRectForRange:" method to work. This code basically takes the last three letters of the UITextView "textStuff" and highlights it green.

UITextView *textStuff = [[UITextView alloc] init];
textStuff.frame = CGRectMake(2.0, 200.0, 200.0, 40.0);
textStuff.text = @"how are you today?";
textStuff.textColor = [UIColor blackColor];

UITextPosition *Pos2 = [textStuff positionFromPosition: textStuff.endOfDocument offset: nil];
UITextPosition *Pos1 = [textStuff positionFromPosition: textStuff.endOfDocument offset: -3];

UITextRange *range = [textStuff textRangeFromPosition:Pos1 toPosition:Pos2];

CGRect result1 = [textStuff firstRectForRange:(UITextRange *)range ];

NSLog(@"%f, %f", result1.origin.x, result1.origin.y);

UIView *view1 = [[UIView alloc] initWithFrame:result1];
view1.backgroundColor = [UIColor colorWithRed:0.2f green:0.5f blue:0.2f alpha:0.4f];
[textStuff addSubview:view1];

运行此代码的结果:

这篇关于在UITextView中获取单词的X和Y坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆