我试图在 WkWebView 中显示我之前下载并存储在字符串中的 html 页面。
这就是我设置 WkWebView 的方式:
webView = WKWebView(frame: self.blankView.frame)
webView.navigationDelegate = self
webView.loadHTMLString(tipShow.htmlString!, baseURL: nil)
view.addSubview(webView)
我想要显示的 html 字符串是:
<html> <style type="text/css"> * { -webkit-touch-callout: none; -webkit-user-select: none; /*
Disable selection/copy in UIWebView */
}
</style> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,
initial-scale=1">
<title>TITLE</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css">
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> <script
src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script> </head>
<body>
<div data-role="page" id="page1">
<div align=justify style="margin-left:20px; margin-right:20px">
<font size="4.5" face="HelveticaNeue-Light"><br>
<p>
THIS IS A TEST
</p>
</div> </div>
</body></html>
当 WkWebView 显示在我的视图中时,它永远保持这样。
有人可以解释一下为什么以及如何解决这个问题吗?
环球银行金融电信协会
你可能把它弄得太复杂了。这对我有用......
首先导入WebKit
其次,在你的类下创建一个@IBOutlet:
@IBOutlet weak var webView: WKWebView!
第三,将以下内容放入 viewDidLoad() 中:
let htmlString:String! = "\(YourString)"
webView.loadHTMLString(htmlString, baseURL: webView.loadHTMLString(htmlString, baseURL: Bundle.main.bundleURL))
我知道现在有点晚了,但这是所请求的 Objective-C 版本:
@property (weak, nonatomic) IBOutlet WKWebView *webView;
[self.webView loadHTMLString:strTemplateHTML baseURL:[NSBundle mainBundle].bundleURL];
WebView 加载 HTML 字符串 - 使其变得简单 - 更新和测试工作 - 代码于 2024 年
import UIKit
import WebKit
class ViewController: UIViewController {
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
print("initiating webview")
let htmlString:String! = "<body style=\"font-family: \'Poppins\', sans-serif;font-size: 48px; font-weight: 400; background-color: #000; color: #fff; margin:0; overflow-x: hidden;\"><div style=\"height: 100%;width: 100%;display: flex;justify-content: center;align-items: center;\"><div style=\"text-align: center; padding: 0 10% 0 10%;\"><h1 style=\"font-size:48px\">Welcome to sample html string load to webview in iOS.</h1><br><br><label style=\"font-size:24px\">HTML Load Webview</label><h1 style=\"font-size:32px; margin-top: 3px;\">#WKWebView</h1><br><label style=\"font-size:24px\">Youtube channel: Swiftkat Code Factory</label><h1 style=\"font-size:32px; margin-top: 3px;\">Let's Learn coding</h1><br><label style=\"font-size:24px\">Programmer</label><h1 style=\"font-size:32px; margin-top: 3px;\">Proud to be an Engineer</h1><br><label style=\"font-size:24px\">Apple Developer</label><h1 style=\"font-size:32px; margin-top: 3px;\">Enjoy Coding!!!</h1><br></div></div></body>"
webView.loadHTMLString(htmlString, baseURL: Bundle.main.bundleURL)
}
}