iOS开发——UI篇Swift篇&UIWebView

时间:2023-03-09 02:28:57
iOS开发——UI篇Swift篇&UIWebView

UIWebView

     //返回按钮事件
     @IBAction func backButtonClick()
     {
         self.navigationController?.popViewControllerAnimated(true)
     }

     override func viewDidLoad() {
         super.viewDidLoad()

         titleLabel.text = titleString

         // Do any additional setup after loading the view.

         //创建UIWebView
         var aWebView:UIWebView = UIWebView(frame: CGRectMake(, , UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height-))

         //添加到视图上
         self.view.addSubview(aWebView)

         //网址字符串
         var webString = "http://www.iphonetrain.com"

         //通过String类型初始化NSURL对象
         var url:NSURL! = NSURL(string: webString)

         //通过NSURL对象初始化NSURLRequest
         var request:NSURLRequest = NSURLRequest(URL: url)

         //webView加载网页
         aWebView.loadRequest(request)

         //设置网页压缩,全屏显示
         aWebView.scalesPageToFit = true

         var htmlString:String = "<font face=黑体 size=7 color=\"red\">无限互联3G学院</font>"

         //baseURL赋值一个资源css的路径,可以用网络路径使用网络上的CSS
 //        aWebView.loadHTMLString(htmlString, baseURL: nil)

         //定义一个常量路径
         let wordPath:String? = NSBundle.mainBundle().pathForResource("SwiftIntroduction", ofType: "docx")

         //通过String类型初始化NSURL对象
         let wordURL:NSURL! = NSURL(string: wordPath!)

         //通过NSURL对象初始化NSURLRequest
         let wordRequest:NSURLRequest = NSURLRequest(URL: wordURL)

         //加载
 //        aWebView.loadRequest(wordRequest)

         //设置网页视图识别的内容
         aWebView.dataDetectorTypes = UIDataDetectorTypes.Address

     }

     override func didReceiveMemoryWarning() {
         super.didReceiveMemoryWarning()
         // Dispose of any resources that can be recreated.
     }

     /*
     // MARK: - Navigation

     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
         // Get the new view controller using segue.destinationViewController.
         // Pass the selected object to the new view controller.
     }
     */

     // MARK: - UIWebViewDelegate

     func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool
     {
         if navigationType == UIWebViewNavigationType.LinkClicked
         {

         }

         return true
     }

     func webViewDidStartLoad(webView: UIWebView)
     {
         //开始加载
     }

     func webViewDidFinishLoad(webView: UIWebView)
     {
         //加载成功结束
     }

     func webView(webView: UIWebView, didFailLoadWithError error: NSError)
     {
         //加载失败
     }