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.
|Published (Last):||16 October 2005|
|PDF File Size:||8.99 Mb|
|ePub File Size:||7.68 Mb|
|Price:||Free* [*Free Regsitration Required]|
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
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.
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?
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.