How to programmatically enter 'selection mode' in a UIWebView?
Asked Answered
A

2

14

I'm trying to create an iBooks like experience, where users can highlight text within a document. To do that, I'm using a UIWebView with my document in an HTML format.

I've figured out how to capture the initial selection highlight, but can't make it so the user can edit or change their highlight once made.

I'm hoping to pre-define a javascript Selection DOM Object with the Range of the highlight and then programmatically enter 'selection mode'. It seems that you can only enter selection mode by long-pressing on a piece of text, and then automatically creates the Selection DOM Object for you.

How do you programmatically enter selection mode with a UIWebView?

Anticlimax answered 6/6, 2013 at 21:38 Comment(6)
On a PC I can modify the selection after it is made by a JavaScript DOM program by using shift-arrowkey etc. I didn't 100% understand your question, are you saying that after using JavaScript to make a selection it is not modifiable by the user on iOS ?Diabolo
Are you just trying to grab text out of the UIWebView? Please let me know your expected goal? It just sounds like your trying to go about it the wrong way and I do have another solution for this. Can you paste the HTML your trying to highlight?Concretize
What I'm hoping to achieve is similar to how iBooks handles re-selecting text that has already been highlighted. You long press to initially create a highlight, drag the pins and then select your style. To EDIT the selection in iBooks, you double tap on the highlight and the blue selection appears with the pins around the text you've highlighted. So, the behavior I'm looking for specifically is how to pre-define the selection range and have the double tap (or a gesture) re-select that block of text with the pins in the right place.Anticlimax
So how did you end up making this work? Or did you?Kareenkarel
No, been to WWDC a few times and spoken with the developers about it. Radar has been submitted, but they still haven't added this functionality yet, not even with the iOS 8 WebKit changes.Anticlimax
I can't find the old bug I submitted to Apple on this, so I just filed a new Radar for it. You can follow it here: openradar.me/18455024Anticlimax
T
1

Does the following SO question help?

Highlight text range using JavaScript

Or look at

https://code.google.com/p/rangy/

Rangy is a cross-browser JavaScript range and selection library.

Ticker answered 10/6, 2013 at 9:51 Comment(1)
No. The UIWebView ignores the Selection and Range objects even if you create them. The problem is that the UIWebView uses a long-press gesture to enter selection mode and then it defines the selection to be whatever the current word being pressed on is.Anticlimax
D
1

You can manipulate the web page by directly executing Javascript strings in it. There is a method:

- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)javascriptString

in UIWebView. This will execute arbitrary script in your page's DOM. Use this with some jQuery maybe you will be able to do that. So maybe you need to interact with your DOM heavily to emulate this.

Determinative answered 14/6, 2013 at 15:31 Comment(1)
No matter how you interact with the DOM, the only way to enter selection mode in a UIWebView is by a long press. And then it won't honor the pre-defined Selection or Range DOM objects, it creates its own.Anticlimax

© 2022 - 2024 — McMap. All rights reserved.