问题描述
我问了一个开发人员(Codea的创建者TwoLivesLeft)他们如何在他们的应用程序中进行语法突出显示。他回答说:
I asked a developer (TwoLivesLeft, the creators of Codea) how they did syntax highlighting in their app. He replied :
任何人都可以解释我如何获得某个单词的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坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!