IOS UIWebView自己编写页面样式

1 class ViewController:UIViewController {
2
3 var webView:UIWebView!
4 override func viewDidLoad() {
5 super.viewDidLoad()
6
7 let bounds = UIScreen.main.bounds
8 let frame = CGRect(x:0, y:40, width:bounds.width,
height:bounds.height-40)
9 webView = UIWebView(frame:frame)
10 webView.backgroundColor = UIColor.clear
11 self.view.addSubview(webView)
12
13 let loadHTML = UIButton(frame:CGRect(x:40, y:
400, width:240, height:44))
14 loadHTML.setTitle(“加载 HTML网页”, for:
UIControlState.init(rawValue:0))
15 loadHTML.backgroundColor = UIColor.brown
16 loadHTML.addTarget(self, action:

selector(ViewController.loadHTML), for:.touchUpInside)

17
18 let loadGIF = UIButton(frame:CGRect(x:40, y:470,
width:240, height:44))
19 loadGIF.setTitle(“加载GIF动画”, for:
UIControlState.init(rawValue:0))
20 loadGIF.backgroundColor = UIColor.brown
21 loadGIF.addTarget(self, action:

selector(ViewController.loadGIF), for:.touchUpInside)

22
23 self.view.addSubview(loadHTML)
24 self.view.addSubview(loadGIF)
25 }
26
27 func loadHTML()
28 {
29 let team = “

20px;’>团队名称:酷课堂

30 let tel = “
电话:15011122222

31 let url = “
网址:
http://www.coolketang.com

32 let html = team + tel + url
33 webView.dataDetectorTypes =
[UIDataDetectorTypes.link,
UIDataDetectorTypes.phoneNumber]
34 webView.loadHTMLString(html, baseURL:nil)
35 }
36
37 func loadGIF()
38 {
39 let resourceURL = Bundle.main.resourceURL
40 webView.loadHTMLString(“ src=‘WildWorld.gif ’>”, baseURL:resourceURL)
41 }
42 }

你可能感兴趣的:(IOS UIWebView自己编写页面样式)