Stephan Heilner
Stephan Heilner

Reputation: 918

How to programmatically enter 'selection mode' in a UIWebView?

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?

Upvotes: 14

Views: 1595

Answers (2)

Maxthon Chan
Maxthon Chan

Reputation: 1189

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.

Upvotes: 1

Uygar Y
Uygar Y

Reputation: 2042

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.

Upvotes: 1

Related Questions