How to display clickable links in UITextView

后端 未结 3 644
轮回少年
轮回少年 2020-12-01 12:36

I am trying to display an attributed string in a UITextview with clickable links. I\'ve created a simple test project to see where I\'m going wrong and still can\'t figure i

相关标签:
3条回答
  • 2020-12-01 13:08

    Swift 3 iOS 10: Here's Clickable extended UITextView that detect websites inside the textview automatically as long as the link start with www. for example: www.exmaple.com if it exist anywhere in the text will be clickable. Here's the class:

    import Foundation
    import UIKit
    
    
    
    
    public class ClickableTextView:UITextView{
    
    
    
        var tap:UITapGestureRecognizer!
        override public init(frame: CGRect, textContainer: NSTextContainer?) {
            super.init(frame: frame, textContainer: textContainer)
            print("init")
            setup()
        }
        required public init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            setup()
        }
    
    
        func setup(){
    
            // Add tap gesture recognizer to Text View
            tap = UITapGestureRecognizer(target: self, action: #selector(self.myMethodToHandleTap(sender:)))
            //        tap.delegate = self
            self.addGestureRecognizer(tap)
        }
    
        func myMethodToHandleTap(sender: UITapGestureRecognizer){
    
            let myTextView = sender.view as! UITextView
            let layoutManager = myTextView.layoutManager
    
            // location of tap in myTextView coordinates and taking the inset into account
            var location = sender.location(in: myTextView)
            location.x -= myTextView.textContainerInset.left;
            location.y -= myTextView.textContainerInset.top;
    
    
            // character index at tap location
            let characterIndex = layoutManager.characterIndex(for: location, in: myTextView.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
    
            // if index is valid then do something.
            if characterIndex < myTextView.textStorage.length {
    
                let orgString = myTextView.attributedText.string
    
    
                //Find the WWW
                var didFind = false
                var count:Int = characterIndex
                while(count > 2 && didFind == false){
    
                    let myRange = NSRange(location: count-1, length: 2)
                    let substring = (orgString as NSString).substring(with: myRange)
    
    //                print(substring,count)
    
                    if substring == " w" || (substring  == "w." && count == 3){
                        didFind = true
    //                    print("Did find",count)
    
                        var count2 = count
                        while(count2 < orgString.characters.count){
    
                            let myRange = NSRange(location: count2 - 1, length: 2)
                            let substring = (orgString as NSString).substring(with: myRange)
    
    //                        print("Did 2",count2,substring)
                            count2 += 1
    
    
                            //If it was at the end of textView
                            if count2  == orgString.characters.count {
    
                                let length = orgString.characters.count - count
                                let myRange = NSRange(location: count, length: length)
    
                                let substring = (orgString as NSString).substring(with: myRange)
    
                                openLink(link: substring)
                                print("It's a Link",substring)
                                return
                            }
    
                            //If it's in the middle
    
                            if substring.hasSuffix(" "){
    
                                let length =  count2 - count
                                let myRange = NSRange(location: count, length: length)
    
                                let substring = (orgString as NSString).substring(with: myRange)
    
                                openLink(link: substring)
                                print("It's a Link",substring)
    
                                return
                            }
    
                        }
    
                        return
                    }
    
    
                    if substring.hasPrefix(" "){
    
                        print("Not a link")
                        return
                    }
    
                    count -= 1
    
                }
    
    
            }
    
    
        }
    
    
        func openLink(link:String){
    
            if let checkURL = URL(string: "http://\(link.replacingOccurrences(of: " ", with: ""))") {
                if UIApplication.shared.canOpenURL(checkURL) {
                    UIApplication.shared.open(checkURL, options: [:], completionHandler: nil)
    
                    print("url successfully opened")
                }
            } else {
                print("invalid url")
            }
        }
    
    
        public override func didMoveToWindow() {
            if self.window == nil{
                self.removeGestureRecognizer(tap)
                print("ClickableTextView View removed from")
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-01 13:23

    For swift3.0

      override func viewDidLoad() {
         super.viewDidLoad()
    
      let linkAttributes = [
            NSLinkAttributeName: NSURL(string: "http://stalwartitsolution.co.in/luminutri_flow/terms-condition")!
            ] as [String : Any]
      let attributedString = NSMutableAttributedString(string: "Please tick box to confirm you agree to our Terms & Conditions, Privacy Policy, Disclaimer. ")
    
      attributedString.setAttributes(linkAttributes, range: NSMakeRange(44, 18))
    
      attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSNumber(value: 1), range: NSMakeRange(44, 18))
    
      textview.delegate = self
      textview.attributedText = attributedString
      textview.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.red]
      textview.textColor = UIColor.white
      }
    
    
      func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        return true
       }
    
    0 讨论(0)
  • 2020-12-01 13:24

    Just select the UITextView in your storyboard and go to "Show Attributes inspector" and select selectable and links. As the image below shows. Make sure Editable is unchecked.

    0 讨论(0)
提交回复
热议问题