0

I'm looking to create the below image;

enter image description here

When a user clicks on the Log in part of the text it will call a function (present another controller for e.g)

I have code currently that uses the "Log in" as a URL however this obviously exits the app and opens safari. I want to have a #selector for this or UITapGestureRecogniser I think.

CURRENT CODE

class LogInTextView: UITextView {

    override var selectedTextRange: UITextRange? {
        get { return nil }
        set {}
    }
    
    override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if gestureRecognizer is UIPanGestureRecognizer {
            return super.gestureRecognizerShouldBegin(gestureRecognizer)
        }
        if let tapGestureRecognizer = gestureRecognizer as? UITapGestureRecognizer,
            tapGestureRecognizer.numberOfTapsRequired == 1 {
            return super.gestureRecognizerShouldBegin(gestureRecognizer)
        }
        if let longPressGestureRecognizer = gestureRecognizer as? UILongPressGestureRecognizer,
            longPressGestureRecognizer.minimumPressDuration < 0.325 {
            return super.gestureRecognizerShouldBegin(gestureRecognizer)
        }
        gestureRecognizer.isEnabled = false
        return false
    }
    
    func configureAttributes() {
        let textAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14, weight: .regular)]
        let linkAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14, weight: .medium)]
        let disclaimerText = NSMutableAttributedString(string: "Have an account already? ", attributes: textAttributes)
        let linkString = NSMutableAttributedString(string: "Log in", attributes: linkAttributes)
        let link = linkString.setAsLink(textToFind: "Log in", linkURL: Link.login)
        if link {
            self.attributedText = NSAttributedString(attributedString: linkString)
        }
        
        disclaimerText.append(linkString)
        self.attributedText = disclaimerText
        
        self.textAlignment = .left
        self.dataDetectorTypes = .link
        self.isScrollEnabled = false
        self.isSelectable = true
        self.isEditable = false
        self.backgroundColor = .clear
        self.textColor = .gray
        self.tintColor = .systemGreen
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        textContainerInset = UIEdgeInsets.zero
        textContainer.lineFragmentPadding = 0
    }
}
David Henry
  • 1,972
  • 20
  • 43
  • You don't need the tap gestures. Just set a valid url, BUT implement the delegate of the UITextView, especially `textView(_:shouldInteractWith:in:interaction:)`. Make it return false if the url si `Link.login`, BUT do your custom action when this happens, like showing a new view controller. – Larme Nov 05 '20 at 16:23
  • 1
    Check this link [https://stackoverflow.com/questions/36043006/swift-tap-on-a-part-of-text-of-uilabel](https://stackoverflow.com/questions/36043006/swift-tap-on-a-part-of-text-of-uilabel) – Pratik Prajapati Nov 05 '20 at 16:26
  • Thanks @PratikPrajapati this is the solution I was looking for. I wasn't sure it was possible in a UILabel until I saw the UITapGestureRecogniser extension. – David Henry Nov 06 '20 at 11:39

0 Answers0