iPhone SDK - opening links in a UITextView in a web view

前端 未结 6 1575
被撕碎了的回忆
被撕碎了的回忆 2021-01-06 18:00

I have specified dataDetectorTypes on a UITextView so that URLs open in Safari when touched.

Is it possible to intercept this behaviour so I load the URL in a UIWebV

6条回答
  •  说谎
    说谎 (楼主)
    2021-01-06 18:44

    Swift version:

    Your standard UITextView setup should look something like this, don't forget the delegate and dataDetectorTypes.

    var textView = UITextView(x: 10, y: 10, width: CardWidth - 20, height: placeholderHeight) //This is my custom initializer
    textView.text = "dsfadsaf www.google.com"
    textView.selectable = true
    textView.dataDetectorTypes = UIDataDetectorTypes.Link
    textView.delegate = self
    addSubview(textView)
    

    After your class ends add this piece: Note that you need https://github.com/TransitApp/SVWebViewController this library, which is the best one out there as far as I know.

    class myVC: UIViewController {
        //viewdidload and other stuff here
    }
    
    extension MainCard: UITextViewDelegate {
        func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
            //Do your stuff over here
            var webViewController = SVModalWebViewController(URL: URL)
            view.presentViewController(webViewController, animated: true, completion: nil)
            return false
        }
    }
    

提交回复
热议问题