本文介绍了可以将自定义 HTML 视图快速加载到 webView 中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我正在将 url 加载到 webView 中,它看起来像:

但我想从这个网址中删除一些不需要的东西,比如:

我在互联网上搜索了很多,但没有找到任何有用的东西.但我认为我必须在加载到 webView 之前从 HTML 视图中删除一些标签.但我不知道该怎么做.

是否有可能,如果有,我该如何执行此操作?

请帮我解决这个问题.

解决方案

我已经找到了解决我的问题的解决方案 教程对我帮助很大.

如本教程所示,这里是将自定义 HTML 视图加载到 webView 中的分步示例.

来自字符串的 Web 视图:

有多种使用网络视图的方法.在属性字符串之前,这是获取格式化文本的唯一方法.开发人员将用 HTML 编写一串文本,并使用 loadHTMLString 方法将其发送到 UIWebview.将 viewDidLoad 方法更改为:

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

第 3 行是一个可选字符串,因为 loadHTMString 方法需要一个.第 4 行加载字符串,并有第二个参数 baseURL,它设置 HTML 的基本 URL 路径.通常我们将其设置为 nil,但是如果您想要加载像图像或样式表这样的文件,这将提供文件的默认路径.构建并运行:

那太无聊了,所以让我们添加更多的 HTML 代码,用这个替换字符串,所有都没有分页符.

letmyHTMLString:String!= "<h1 style=\"font-family: Helvetica\">Hello Pizza</h1><p>点按上面的按钮以查看<strong>一些很酷的东西</strong>和<code>UIWebView</code><p><img src=\"https://apppie.files.wordpress.com/2014/09/photo-sep-14-7-40-59-pm_small1.jpg\">;"

您会注意到我们在几个地方使用了 Quote 转义序列 \".我们需要在几个地方添加引号,反斜杠后跟引号允许我们在字符串中这样做.构建并运行.那是一个好一点:

来自文件的 Web 视图和 CSS:

尝试从字符串格式化一些东西并不容易.如果我们将 HTML 放在单独的文件中,我们的工作会更容易.更好的是外部样式表.点击键盘上的 Command-N 或从下拉菜单中点击 File>New>File... 以创建一个新文件.在 iOS 和 Empty 下转到 Other 来制作一个空白文件.

单击下一步,然后将文件命名为 myHTML.html.保存文件.在空白文件中添加以下代码:

<!-- 基于来自 http://www.w3.org/Style/Examples/011/firstcss 的课程 -->Hello Pizza Home<!-- 站点导航菜单--><div class="navbar"><a href="index.html">首页</a><a href="musings.html">Toppings</a><a href="town.html">地壳</a><a href="links.html">烤箱</a>

<div class="内容"><!-- 主要内容--><h1>你好披萨!</h1>这是一个热爱所有比萨的网站!但我们不仅仅是披萨,我们还有大饼上的任何东西.所以环顾四周,你也会发现印度烤饼、扁面包、皮塔饼和玉米饼相关的东西!!!如果这还不够,请查看我们的姊妹网站<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=""/><!-- 在页面上签名并注明日期,这只是礼貌!-->

<地址>制作于 2012 年 10 月 27 日来自 makeapppie.com.</address>

返回 ViewController.swift 文件,将 helloPizza 方法更改为:

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

第 2 行从所谓的主包中为 myHTML.html 创建了一个 url.主包是我们将所有代码文件放置在特定项目的 XCode 中的目录.主捆绑包根据设备更改位置.我们不提供文字路径,而是使用此相对指示符.第 3 行创建了一个 NSURLRequest,这是我们在第 4 行中提供给 loadRequest 方法的对象. loadRequest 将 HTML 代码加载到 UIWebView 中.构建并运行它.点击 Pizza 按钮,你会得到这个:

这个视图不是那么漂亮,但我们可以添加一些 CSS 来清理它.像之前一样创建另一个空白文件,命名为 myCSS.css 并将以下内容添加到新文件中:

body {颜色:#eeeeee;背景:#a0a088;字体系列:Helvetica}h1{颜色:#dd1111;字体系列:粉笔灰;字体大小:18pt}一种{字体系列:Helvetica;字体粗细:粗体;颜色:#ffffaa;文字装饰:无;padding-left: 5px;}图片{填充左:0;最大宽度:90%;最大高度:90%;框阴影:3px 3px 3px 0px #202020}.导航栏{背景色:#000000;白颜色;位置:绝对;顶部:0em;左:0em;宽度:100% }.内容{填充左:1em;填充顶部:1em;}

myHTML.Html 中的 HTML 代码假定 baseURL 是包.构建并运行,我们会得到一个格式更好的网络视图:

现在有了这个例子,我可以从浏览器复制页面源代码,然后我可以将该 HTML 代码粘贴到我的 HTML 文件中,我可以进行更改,例如我可以从该 HTML 代码中删除不需要的标签,然后我可以加载这个 HTML进入webView.

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.

解决方案

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.

这篇关于可以将自定义 HTML 视图快速加载到 webView 中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 15:59