June 24, 2020

You should not embed UIWebView or UITableView objects in UIScrollView it attempts to preserve its URL history, the scaling and scrolling positions for each. Explains how to create and manage files and directories. If you want to generate HTML locally and show it inside your app, it’s easy to do in both UIWebView and WKWebView. First, here’s the code for.

Author: Darn Malalabar
Country: Belize
Language: English (Spanish)
Genre: Music
Published (Last): 16 October 2005
Pages: 409
PDF File Size: 8.99 Mb
ePub File Size: 7.68 Mb
ISBN: 691-1-60467-869-9
Downloads: 79237
Price: Free* [*Free Regsitration Required]
Uploader: Goltigul

The found occurrences of the searched text are highlighted with a yellow background, so the search result can be visually located very easy. This blog post describes how this can be implemented. Our goal is to implement two methods for our new UIWebView category.

Search and highlight text in UIWebView

One method should start the search and highlights the search results. The other method should remove all the highlighted search results again to restore the original layout of the web page. What we need to do is to write uiewbview main code in JavaScript and a wrapper code in Objective C which is simply calling the JavaScript code.

We start with the JavaScript code that is doing the real work. The basic principle of searching the text and removing the highlighted search results is the same: All the nodes are linked together with parent and child connections. This element has usually two children: Only the content of the BODY element is visible and displayed on screen, so we only need to process this part of the document tree. What we need to do is to start with the body element and traverse all of its child nodes.

From within the child nodes we need to go to their child nodes as well, and so on until we reach a leaf nodes, which has no child elements.

The DFS algorithm will traverse the tree structure starting from a root element in our case the BODY element to the first leaf node in a branch of the tree for example going to the first child of the root first, from there again going to the first child, etc until covoa leaf node is reached.

Then the algorithm goes back backtracking to the last node where not all child nodes were traversed yet and continues with the next unvisited child nodes etc. This uiwebvieww all nodes of the whole tree are traversed and we are able to find all the text nodes in which we are looking for the text we are earching. The initialization for a new search is to make sure than no highlighted text from a previous search is present, uuwebview we simple call the function to remove all text highlights.

When sve for a text, we check if the currently inspected node is a text node or if it is an element node. If it is an element node swve can have child nodes, and these must be inspected as well. If yes, we insert the text highlight, otherwise we are finished with this node.

When the text of a text node contains the searched aa, we have to split the text into three parts. Part one will contain the text up to the searched text, part two contains the searched text itself and part three contains the rest of the text. A new element will be created a SPAN element and the second part the searched text will become a child node of this new element.


Now we can assign StyleSheet rules to the newly created SPAN element to create the highlight effect setting the background color to yellow, wave the text color to black, you can even increase the font size, if you want to.

Now the new element is linked with part one and three so it becomes a part of the tree strucuture of the HTML tree.

cocoa touch – Save Image From UIWebView into Photo Library in iOS – Stack Overflow

And because the searched text might be found multiple times in the original text coocoa, we continue to search for the searched text in the third part of the original text node.

If we find another occurrence of the searched text, we split this third part again in three parts, otherwise we are finished. A value of 3 means that the node is a text node.

In this case the property nodeValue contains the text of the node. In our case only the values 1 element node and 3 text node are important. The reason for this is to avoid any conflicts with existing function and variable names of the web page in which we inject our JavaScript code.

But if you have to deal with HTML and JavaScript code of any web pages like in a web browser like iCab Mobileyou should use longer names and also add the name of your app as Prefix to all function and variable names to avoid any conflicts. We only need to declare the interface and write a simple wrapper which loads and calls the JavaScript code that is actually doing all the hard work. The interface is also simple, we only need two methods: The typical use case would be to provide a search field where the user can enter some text.


Loading the external JavaScript file and injecting it into the Web page is not necessary here. In case your App has to deal with web pages which can have frames, you have to add some additional code that looks for frames.

You have to traverse all the documents of all the frames to find all the text nodes in all frames. But I have few queries. Can we achieve following things in HTML book using javascript? Can I store the location of each result of searched text? I want to store the location because when Application restarts I would be reapplying those annotations highlight, underline, strikethrough.

Amey Gadre First of all, you need to be aware that web sites do change from time to time, so you have to be prepared that your saved locations could become invalid any time. Also you can not store references to site elements outside of the context of the web site easily, because the next time you load the page, these references will be different. But assuming you can detect that a page has changed for example by checking the last modification date in the HTTP headers, the content size etcyou can simply count the number of occurrences of the search term and the index of the locations which you want to highlight.


When you load the page later again, do the search again, count the occurrences again and then it should be easy to highlight the correct page elements again. The way you highlight anything is up to you. In the above example I only changed the background color, but you can apply any HTML or CSS layout property here, so you can also underline and strikethrough.

Thank for this post, it works only in html fileBut I have to highlight on pdf file, please let me know any other solution. But i need to search and highlight text in WKWebView, do you have any information about that?

TWebKit on

For WKWebView this works more or less the same way. You can pass the same JavaScript code to this method, and you get the result back in the completion handler. And because highlighting requires to insert additional HTML elements, the number of child elements can go up.

In order to go back and forward you probably better create an array of the highlighted elements within the highlighting method from above. So you get a list of all occurrences, and when going back or forward you only need to decrement or increment an index which represents the current element from the array, and then make the background of the previously selected item transparent and the background of the new element yellow for example.

Hi Alex, I want to fix the search traverse logic and the sequesnce of putting span ids Have a look at my code: But I guess the easiest solution to your problem would be to solve the enumeration for the ID attribute in a completely separate step. So in the first step do what the scrip from this blog post is doing: And in a second step traverse the HTML tree again, this time without doing any modifications of the tree structure and from the beginning to the end. So when doing this in depth-first-order, you would find the SPANs with the occurrences in the correct order, so if you set the ID attribute now, they should be in the correct order and you should be able to avoid the randomness when going back or forward.

I used this combined with a navigation through the found items by using an array like you said.

Thank you for such a wonderful tutorial. It helped me lot. I want to scroll web view to the first occurrence of matched string. Yogita Yes, this is possible. Your uiwehview address will not be published.

In the above implementation, we count the number of found occurrences in a global variable. The implementation would look like this: Thank you for quick respond Alexander.

It works as expected, savd you for this great tutorial. Thanks for your idea and sample code. It still works perfectly in Leave a Reply Cancel reply Your email address will not be published.

Posted in Education