IOS UIWebView自己编写页面样式

2019-07-08 12:58:29 浏览数 (1)

代码语言:javascript复制
 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 = “<div style=‘color:#ff0000;font-size:
 20px;’>团队名称:酷课堂</div>”
 30 let tel = “<div>电话:15011122222</div>”
 31 let url = “<div><b>网址:
 http://www.coolketang.com</b></div>”
 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(“<img
 src=‘WildWorld.gif ’>”, baseURL:resourceURL)
 41 }
 42 }

0 人点赞