I have a signup page in my app and I want users to be able to view terms of service (TermsOfServiceViewController) and privacy policy (PrivacyPolicyViewController) when user pressed text inside my UILabel.
My statement is as such: "By checking this box, you agree to our Terms of Service and our Privacy Policy". When user presses "Terms of Service" I want them to see TermsOfServiceViewController and when user presses "Privacy Policy", I want them to see PrivacyPolicyViewController.
Right now my code is like this:
override func viewDidLoad() {
super.viewDidLoad()
let termsOfServiceViewController = NavigationTapGestureRecognizer(target: self, action: #selector(termsOfServiceVCTapped))
termsOfServiceViewController.viewController = self
let privacyPolicyViewController = NavigationTapGestureRecognizer(target: self, action: #selector(privacyPolicyVCTapped))
privacyPolicyViewController.viewController = self
let string = NSMutableAttributedString(string: "By checking this box, you agree to our ")
let attributedTermsOfService = NSMutableAttributedString(string: "Terms of Service", attributes: [NSAttributedString.Key.link: termsOfServiceViewController, NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue, NSAttributedString.Key.underlineColor: UIColor.appColor(LPColor.LightestPurple)!, NSAttributedString.Key.foregroundColor: UIColor.appColor(LPColor.LightestPurple)!])
let additionalString = NSMutableAttributedString(string: " and our ")
let attributedPrivacyPolicy = NSMutableAttributedString(string: "Privacy Policy", attributes: [NSAttributedString.Key.link: privacyPolicyViewController, NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue, NSAttributedString.Key.underlineColor: UIColor.appColor(LPColor.LightestPurple)!, NSAttributedString.Key.foregroundColor: UIColor.appColor(LPColor.LightestPurple)!])
string.append(attributedTermsOfService)
string.append(additionalString)
string.append(attributedPrivacyPolicy)
agreementLabel.attributedText = string
agreementLabel.addGestureRecognizer(termsOfServiceViewController)
agreementLabel.addGestureRecognizer(privacyPolicyViewController)
agreementLabel.isUserInteractionEnabled = true
}
@objc func termsOfServiceVCTapped() {
let vc = TermsOfServiceViewController.storyboardInstance(storyboardName: "Login") as! TermsOfServiceViewController
navigationController?.pushViewController(vc, animated: true)
}
@objc func privacyPolicyVCTapped() {
let vc = PrivacyPolicyViewController.storyboardInstance(storyboardName: "Login") as! PrivacyPolicyViewController
navigationController?.pushViewController(vc, animated: true)
}
Right now this code is allowing tap on entire UILabel (I believe this is because I am adding addGestureRecognizer to entire agreementLabel) and it’s only going to privacyPolicyViewController. Is there any way I can separate them from one another and only detect tap when user taps the actual text not the entire label?
Also, my NavigationTapGestureRecognizer looks like this:
class NavigationTapGestureRecognizer: UITapGestureRecognizer {
var viewController: UIViewController?
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event!)
}
}
If there are any suggestion that I can get, that would be fantastic. Thanks in advance.
2
Answers
So after researching a bit, I figured out the solution. Please see below:
Create extension for UITapGestureRecognizer:
Then add attributes to string and UITapGestureRecognizer:
Referenced this link: Tap on a part of text of UILabel