標籤:
UIWebView
1 //返回按鈕事件 2 @IBAction func backButtonClick() 3 { 4 self.navigationController?.popViewControllerAnimated(true) 5 } 6 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 11 titleLabel.text = titleString 12 13 14 // Do any additional setup after loading the view. 15 16 17 //建立UIWebView 18 var aWebView:UIWebView = UIWebView(frame: CGRectMake(0, 65, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height-65)) 19 20 21 //添加到視圖上 22 self.view.addSubview(aWebView) 23 24 //網址字串 25 var webString = "http://www.iphonetrain.com" 26 27 //通過String類型初始化NSURL對象 28 var url:NSURL! = NSURL(string: webString) 29 30 //通過NSURL對象初始化NSURLRequest 31 var request:NSURLRequest = NSURLRequest(URL: url) 32 33 //webView載入網頁 34 aWebView.loadRequest(request) 35 36 37 //設定網頁壓縮,全螢幕顯示 38 aWebView.scalesPageToFit = true 39 40 41 var htmlString:String = "<font face=黑體 size=7 color=\"red\">無限互聯3G學院</font>" 42 43 //baseURL賦值一個資源css的路徑,可以用網路路徑使用網路上的CSS 44 // aWebView.loadHTMLString(htmlString, baseURL: nil) 45 46 47 //定義一個常量路徑 48 let wordPath:String? = NSBundle.mainBundle().pathForResource("SwiftIntroduction", ofType: "docx") 49 50 //通過String類型初始化NSURL對象 51 let wordURL:NSURL! = NSURL(string: wordPath!) 52 53 //通過NSURL對象初始化NSURLRequest 54 let wordRequest:NSURLRequest = NSURLRequest(URL: wordURL) 55 56 //載入 57 // aWebView.loadRequest(wordRequest) 58 59 60 //設定網頁視圖識別的內容 61 aWebView.dataDetectorTypes = UIDataDetectorTypes.Address 62 63 } 64 65 override func didReceiveMemoryWarning() { 66 super.didReceiveMemoryWarning() 67 // Dispose of any resources that can be recreated. 68 } 69 70 71 /* 72 // MARK: - Navigation 73 74 // In a storyboard-based application, you will often want to do a little preparation before navigation 75 override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { 76 // Get the new view controller using segue.destinationViewController. 77 // Pass the selected object to the new view controller. 78 } 79 */ 80 81 // MARK: - UIWebViewDelegate 82 83 func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool 84 { 85 if navigationType == UIWebViewNavigationType.LinkClicked 86 { 87 88 } 89 90 return true 91 } 92 93 func webViewDidStartLoad(webView: UIWebView) 94 { 95 //開始載入 96 } 97 98 func webViewDidFinishLoad(webView: UIWebView) 99 {100 //載入成功結束101 }102 103 func webView(webView: UIWebView, didFailLoadWithError error: NSError)104 {105 //載入失敗106 }
iOS開發——UI篇Swift篇&UIWebView