How can I create a “hyperlink” with Swift?

前端 未结 3 794
伪装坚强ぢ
伪装坚强ぢ 2021-01-07 03:53

I\'m trying to make separate pieces of text UILabels clickable. What I\'m looking for is commonly known as a hyperlink in web development.



        
相关标签:
3条回答
  • 2021-01-07 04:25

    The One approach would be something like the following.
    The assumptions are:

    • self.urls is a string array containing the urls associated with each UILabel.
    • Each UILabel tag has been set to the corresponding index in the array
    • labelTapped: is set as the touchUpInside handler for the labels.
    import Foundation
    import UIKit
    
    class urltest {
    
        var urls:[String]
    
        init() {
            self.urls=[String]()  // Load URLs into here
        }
    
        @IBAction func labelTapped(sender:UILabel!) {
    
            let urlIndex=sender.tag;
            if (urlIndex >= 0 && urlIndex < self.urls.count) {
               self.openUrl(self.urls[urlIndex]);
            }
    
        }
    
        func openUrl(url:String!) {
    
            let targetURL=NSURL.URLWithString(url)
    
            let application=UIApplication.sharedApplication()
    
            application.openURL(targetURL);
    
        }
    }
    
    0 讨论(0)
  • 2021-01-07 04:33

    Swift 3 I created a LinkUILabel class in github: https://github.com/jorgecsan/LinkUILabel With this you only need add the url inspectable as the shows the image: or assign the url variable programmatically:

    linkUILabel.url = "www.example.com"
    

    If you want to implement by your self also I found that solution!:)

    using:

    // This is the label
    @IBOutlet weak var label: UILabel!
    
    override func loadView() {
        super.loadView()
    
        // This is the key
        let tap = UITapGestureRecognizer(target: self, action: #selector(self.onClicLabel(sender:)))
        label.isUserInteractionEnabled = true
        label.addGestureRecognizer(tap)
    }
    
    // And that's the function :)
    func onClicLabel(sender:UITapGestureRecognizer) {
        openUrl("http://www.google.com")
    }
    
    
    func openUrl(urlString:String!) {
        let url = URL(string: urlString)!
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(url)
        }
    }
    

    Hope it helps!:)

    0 讨论(0)
  • 2021-01-07 04:34

    Hyperlink via UITextView

    var termsConditionsTextView: UITextView = {
    let view = UITextView()
     view.backgroundColor = .clear
     view.textAlignment = .left
     
     let firstTitleString = "By registering for THIS_APP I agree with the "
     let secondTitleString = "Terms & Conditions"
     let finishTitleString = firstTitleString + secondTitleString
     let attributedString = NSMutableAttributedString(string: finishTitleString)
     attributedString.addAttribute(.link, value: "https://stackoverflow.com", range: NSRange(location: firstTitleString.count, length: secondTitleString.count))
     
     view.attributedText = attributedString
     view.textContainerInset = .zero
     view.linkTextAttributes = [
         .foregroundColor: UIColor.blue,
         .underlineStyle: NSUnderlineStyle.single.isEmpty
     ]
     
     view.font = view.font = UIFont(name: "YOUR_FONT_NAME", size: 16)
     view.textColor = UIColor.black
     
     return view }()
    
    0 讨论(0)
提交回复
热议问题