iOS开发——UI篇Swift篇&UIWebView

 

 

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     }

 

 

你可能感兴趣的:(UIWebView)