0

I want to create tappable texts for my terms and conditions. But there are 2 parts of it as follows:

By continuing, you are agreed to the Terms of Service and Privacy Policy.

I tried using this method, but it gave me a wrong answer. Meaning if I tapped on the terms of service, it printed Privacy Policy or just somewhere.

@objc func tapLabel(_ gesture:UITapGestureRecognizer){
        guard let text = instructionLabel.text else { return }
        print("gesture : \(gesture)")
        
                let range1 = (text as NSString).range(of: "the Terms of Service")
                let range2 = (text as NSString).range(of: " and ")
                let range3 = (text as NSString).range(of: "Privacy Policy")
                if gesture.didTapAttributedTextInLabel(label: instructionLabel, inRange: range1) {
                    print("the Terms of Service")
                } else if gesture.didTapAttributedTextInLabel(label: instructionLabel, inRange: range2) {
                    print(" and ")
                } else if gesture.didTapAttributedTextInLabel(label: instructionLabel, inRange: range3){
                    print("Privacy Policy")
                } else {
                    print("Somewhere else")
                }
    }

I'm looking forward to hearing from you. Thank you.

Punreach Rany
  • 2,560
  • 7
  • 23
  • 56

1 Answers1

0

Try using UITextView.

    @IBOutlet weak var agreementTextView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()
        agreementTextView.delegate = self
        agreementTextView.addHyperLinksToText(originalText: "By continuing, you are agreed to the Terms of Service and Privacy Policy.", hyperLinks: [" the Terms of Service" : "SOME_URL", "Privacy Policy." : "OTHER_URL"])
}

extension UITextView {

  func addHyperLinksToText(originalText: String, hyperLinks: [String: String]) {
    let style = NSMutableParagraphStyle()
    style.alignment = .left
    let attributedOriginalText = NSMutableAttributedString(string: originalText)
    for (hyperLink, urlString) in hyperLinks {
        let linkRange = attributedOriginalText.mutableString.range(of: hyperLink)
        let fullRange = NSRange(location: 0, length: attributedOriginalText.length)
        attributedOriginalText.addAttribute(NSAttributedString.Key.link, value: urlString, range: linkRange)
        attributedOriginalText.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: fullRange)
        attributedOriginalText.addAttribute(NSAttributedString.Key.font, value: UIFont(name: "Gotham Pro", size: 12)!, range: fullRange)
    }

    self.linkTextAttributes = [
        NSAttributedString.Key.foregroundColor: #colorLiteral(red: 0.1960784346, green: 0.3411764801, blue: 0.1019607857, alpha: 1),
        NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue,
    ]
    self.attributedText = attributedOriginalText
  }
}

NoName555
  • 235
  • 1
  • 3
  • 8
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 08 '21 at 08:38
  • @NoName555 But this would be a link instead of a touchable text. I want these texts to behave like a button so that I can apply some action. – Punreach Rany Nov 09 '21 at 00:08