It is posible to load customise HTML view into webView in swift?

无人久伴 提交于 2019-12-22 14:40:13

问题


suppose I am loading url into webView and it looks like:

But I want to remove some unwanted thing from this url like:

I searched a lot on internet but nothing helpful found.but I think that I have to remove some of tag from HTML view before it's load into webView. but I don't know how to do this.

Is it possible and if yes then how can I perform this action?

Please help me for this.


回答1:


I have found the solution for my question and for that THIS tutorial helps me a lot.

as shown into this tutorial here is the step by step example for load a customised HTML View into webView.

A Web View From a String:

There are several ways to use a web view. Before attributed strings, this was the only way to get formatted text. The developer would write a string of text in HTML and send it to a UIWebview using the method loadHTMLString. Change the viewDidLoad method to this:

overridefuncviewDidLoad() {
    super.viewDidLoad()
    letmyHTMLString:String! = "<h1>Hello Pizza!</h1>"
    myWebView.loadHTMLString(myHTMLString, baseURL: nil)
}

Line 3 makes a optional string, since the loadHTMString method requires one. Line 4 loads the string, and has a second parameter baseURL, which sets the base URL path for the HTML. Usually we set this to nil but if you had files you wanted to load like images or a style sheet, this would give the default path for the files. Build and run:

That was pretty boring, so let’s add some more HTML code, replace the string with this, all without a page break.

letmyHTMLString:String! = "<h1 style=\"font-family: Helvetica\">Hello Pizza</h1><p>Tap the buttons above to see <strong>some cool stuff</strong> with <code>UIWebView</code><p><img src=\"https://apppie.files.wordpress.com/2014/09/photo-sep-14-7-40-59-pm_small1.jpg\">"

Youll notice a few places where we used the Quote escape sequence \". WE need to add quotes in a few pplaces and a backslash followed by a quote allows us to do so within a string. Build and Run. That is a little better:

A Web View and CSS From Files:

Trying to make something formatted from a string is not easy. Our work would be easier if we had the HTML in a separate file. Even better would be an external style sheet. Tap Command-N on the keyboard or File>New>File… from the drop-down menu to make a new file. Make a blank file by going to Other under iOS and Empty.

Click next and then name the file myHTML.html. Save the file. In the blank file add the following code:

<!--<DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<!-- Based on lesson from http://www.w3.org/Style/Examples/011/firstcss -->Hello Pizza Home
 
        <!-- Site navigation menu -->
<div class="navbar">
            <a href="index.html">Home</a>
            <a href="musings.html">Toppings</a>
            <a href="town.html">Crust</a>
            <a href="links.html">Ovens</a>
</div>
<div class="content">
        <!-- Main content -->
<h1>Hello Pizza!</h1>
This is a site for the love of all things pizza!
 
But we are not just pizza, we are with anything on a flatbread. So look around and you will find naan, flatbreads, pitas and tortilla related stuff too!!!
 
If that is not enough, check our sister site <a href="pie">Hello Pie</a>
 
<img src="https://apppie.files.wordpress.com/2014/09/photo-sep-14-7-40-59-pm_small1.jpg" alt="" />
        <!-- Sign and date the page, it's only polite! -->
</div>
<address>Made 27 October 2012
by makeapppie.com.</address>

Back in the ViewController.swift file change the helloPizza method to this:

@IBAction func helloPizza(sender: UIBarButtonItem) {
let myURL = NSBundle.mainBundle().URLForResource("myHtml", withExtension: "html")
let requestObj = NSURLRequest(URL: myURL!)
myWebView.loadRequest(requestObj)
}

Line 2 creates a url for myHTML.html from what is called the main bundle. The main bundle is the directory we place all of our code files in XCode for a specific project. The main bundle changes location depending on the device. Instead of giving a literal path, we use this relative indicator. Line 3 creates an NSURLRequest, an object we feed into the loadRequest method in line 4. The loadRequest loads the HTML code into the UIWebView. Build and run this. Click the Pizza button and you get this:

This view is not that pretty, but we can add some CSS to clean it up a bit. Create another blank file as you did before, named myCSS.css and add the following to the new file:

body {
color: #eeeeee;
background: #a0a088;
font-family:Helvetica}
h1 {
color: #dd1111;
font-family:Chalkduster;
font-size: 18pt}
a{
font-family:Helvetica;
font-weight: bold;
color: #ffffaa;
text-decoration:none;
padding-left: 5px;
}
img{
padding-left:0;
max-width: 90%;
max-height: 90%;
box-shadow: 3px 3px 3px 0px #202020
}
.navbar {
background-color: #000000;
color: white;
position: absolute;
top: 0em;
left: 0em;
width: 100% }
.content{
padding-left: 1em;
padding-top: 1em;
}

The HTML code in myHTML.Html assumes that the baseURL is the bundle. Build and run and we get a better formatted web view:

Now with this example I can copy the page source from the browser and then I can paste that HTML code into my HTML file and I can make changes like I can remove unwanted tag from that HTML code and later I can load this HTML into webView.



来源:https://stackoverflow.com/questions/27561588/it-is-posible-to-load-customise-html-view-into-webview-in-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!