Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attributedLabel(_ label: NantesLabel, didSelectLink link: URL) doesn't seem to be working #8

Closed
spadafiva opened this issue Feb 27, 2019 · 8 comments

Comments

@spadafiva
Copy link
Contributor

First off, thanks so much for making an awesome looking label replacement. In our app I'm currently doing a lot of work to manually calculate sizes in a UITextview for fields rendering markdown. I think this might be a great replacement if I can work out some of the kinks.

I'm currently using this to try to make something like this:

screen shot 2019-02-27 at 4 04 26 pm

Anyway, thanks in advance for making it, and hopefully it's just user error on my part.

Describe the bug
Cannot get a link to trigger the delegate didSelectLink callback.

To Reproduce
Create a view controller and add code like this:

class ViewController: UIViewController {

    var termsAndConditionsLabel: NantesLabel!


    // MARK: - Life Cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        setupTermsLabel()
    }

    // MARK: - Setup
    private func setupTermsLabel() {
        termsAndConditionsLabel = NantesLabel(frame: .zero)
        termsAndConditionsLabel.numberOfLines = 0
        view.insertSubview(termsAndConditionsLabel, aboveSubview: stackView)
        termsAndConditionsLabel.snp.makeConstraints { $0.top.equalToSuperview().offset(100); $0.left.equalToSuperview(); $0.right.equalToSuperview() }
        let termsText = "I understand by tapping \"Sign up\" my information will be used as described here and in the Privacy Policy, and I agree to the Terms."
        termsAndConditionsLabel.delegate = self
        termsAndConditionsLabel.text = termsText
        let url = URL(string: "https://www.google.com")!
        let privacyPolicyText = "Privacy Policy"
        let privacyPolicyRange = (termsText as NSString).range(of: privacyPolicyText)
        termsAndConditionsLabel.addLink(to: url, withRange: privacyPolicyRange)
    }
}

extension ViewController: NantesLabelDelegate {
    func attributedLabel(_ label: NantesLabel, didSelectLink link: URL) {
        print(link)
    }
}

Expected behavior
I expected to see the link that was tapped printed to the console.

Smartphone (please complete the following information):

  • Device: iOS Simulator on Xcode 10.1 (iPhoneX)
  • OS: 12.1
@chansen22
Copy link
Contributor

TL;DR, it doesn't look like you're doing anything wrong as far as Nantes is concerned. (Guessing here) I would take a look at the frame of your label to make sure the constraints are correct, so when you tap, you're tapping inside the label's frame still.

Longer version:
I put almost exactly what you pasted here into the example project and it seems to work fine for me.

Here's my version:

private func setupTermsLabel() {
        let termsAndConditionsLabel = NantesLabel(frame: .zero)
        termsAndConditionsLabel.numberOfLines = 0
        labelStackView.addArrangedSubview(termsAndConditionsLabel)
        let termsText = "I understand by tapping \"Sign up\" my information will be used as described here and in the Privacy Policy, and I agree to the Terms."
        termsAndConditionsLabel.delegate = self
        termsAndConditionsLabel.text = termsText
        let url = URL(string: "https://www.google.com")!
        let privacyPolicyText = "Privacy Policy"
        let privacyPolicyRange = (termsText as NSString).range(of: privacyPolicyText)
        termsAndConditionsLabel.addLink(to: url, withRange: privacyPolicyRange)
    }

I removed the constraints you had setup with SnapKit, since my stackview will handle laying the label out for me, and changed the add to subview, since it's going into the stackview instead of a view, but that shouldn't bother anything related to the link working.

If I had to guess around your issue, maybe the frame is not set correctly for the label in your view? If you use the view inspector and take a look at the frame around it, is it painting outside the frame (so it can't handle taps, since you might be tapping outside of it)?

My example project in the repo has something almost exactly like the one you're trying to recreate. The setupPrivacyPolicyLabel function.

@chansen22
Copy link
Contributor

chansen22 commented Feb 27, 2019

Hmm I made a new project with SnapKit and Nantes, it looks like it's working fine for me there too. I copied all your code except the stackview part inside view.insertSubview(termsAndConditionsLabel, aboveSubview: stackView) I just used view.addSubview(termsAndConditionsLabel).

So maybe the issue is that it's still behind another view and it can't capture taps? I'm not really sure what's blocking you here.

Just a quick sanity question, you're tapping "Privacy Policy" in the label right? Not "Sign Up"? :)

@spadafiva
Copy link
Contributor Author

Right. That's why I was confused. In my actual project I have it in a stackview embedded in a scroll view without using snapkit at all. I removed it and tried the snapkit layout to rule out the stackview and scroll view as an issue. I'll try checking out the view inspector and let you know either way.

@spadafiva
Copy link
Contributor Author

Also, I've tried tapping literally every pixel of the label. :)

@spadafiva
Copy link
Contributor Author

Okay, so it seems like the problem was an unrelated problem...I had a tap gesture recognizer set on the view to dismiss the keyboard when a user taps outside of any of the textfields on the form. It seems like that's overriding the touch handling of the NantesLabel.

I would like to preserve that behavior if possible. I've tried gestureShouldRecognizeSimultaneouslyWith, but that didn't seem to help. Maybe I can make something happen by fiddling with the hit tests for some of my views. I'm going to dig into this some more, but it seems like it's not a Nantes issue probably, except perhaps an part of an FAQ or something for pitfalls.

@spadafiva
Copy link
Contributor Author

Thanks for taking a look!

@chansen22
Copy link
Contributor

Thanks for the issue! I'll start putting the FAQ together.

@spadafiva
Copy link
Contributor Author

Cool, let me know if you'd like any help with it. I'd be happy to help work with you to draft it or to proofread or anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants