I use a UIWebView to display text with embedded links in my application. I’ve got a method that detects taps in the UIWebView, but I want to figure out how to detect what character / part of text in the note was tapped.
When a user taps, the text, it’ll open a UITextView with that same text, and I’d like to place the cursor at the position of the tap.
So far, I’ve figured out how to get the X and Y position as follows:
NSInteger xInt = [x integerValue];
NSInteger yInt = [y integerValue] + (int)webView.scrollView.contentOffset.y;
I’ve also got the text as follows:
UILabel *noteTextLabel = [[UILabel alloc] init];
noteTextLabel.font = [UIFont systemFontOfSize:TEXT_FONT_SIZE_LARGE];
noteTextLabel.text = note.text;
How would I detect which character was tapped, based on the X and Y position?
I figured out how to do it, based on this question.
In the
viewControllerthat is loaded with theUITextView, I pass an attribute calledpointForCursorthat’s aCGPointwith the X and Y as mentioned above.Then in
viewWillAppear, I run the following:NOTE: My UIWebView and this UITextView use the same size font, so there weren’t any concerns there.