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?

rmaddy
  • 298,130
  • 40
  • 468
  • 517
Stephan Heilner
  • 808
  • 7
  • 10
  • 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 ? – Woody Jun 09 '13 at 20:12
  • 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? – Bill Thompson Jun 10 '13 at 17:15
  • 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. – Stephan Heilner Jun 11 '13 at 18:45
  • So how did you end up making this work? Or did you? – Lizza Feb 14 '14 at 21:52
  • 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. – Stephan Heilner Sep 16 '14 at 16:26
  • 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: http://www.openradar.me/18455024 – Stephan Heilner Sep 25 '14 at 16:02

2 Answers2

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.

Community
  • 1
  • 1
Uygar Y
  • 1,812
  • 1
  • 12
  • 16
  • 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. – Stephan Heilner Jun 11 '13 at 18:50
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.

Maxthon Chan
  • 1,165
  • 8
  • 14
  • Thanks for giving this a shot. – HalR Jun 16 '13 at 21:31
  • 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. – Stephan Heilner Sep 12 '13 at 16:58