Swift/UIKit 在 UILabel 中单击时推送到新的视图控制器

问题描述 投票:0回答:0

我的应用程序中有一个注册页面,我希望用户在我的 UILabel 中按下文本时能够查看服务条款 (TermsOfServiceViewController) 和隐私政策 (PrivacyPolicyViewController)。

我的声明是这样的:“勾选此框,即表示您同意我们的服务条款和隐私政策”。当用户按下“服务条款”时,我希望他们看到 TermsOfServiceViewController,当用户按下“隐私政策”时,我希望他们看到 PrivacyPolicyViewController。

现在我的代码是这样的:

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)
    }

现在这段代码允许点击整个 UILabel(我相信这是因为我将 addGestureRecognizer 添加到整个 agreementLabel)并且它只会转到 privacyPolicyViewController。有什么办法可以将它们彼此分开,并且仅在用户点击实际文本而不是整个标签时检测点击?

此外,我的 NavigationTapGestureRecognizer 看起来像这样:

class NavigationTapGestureRecognizer: UITapGestureRecognizer {
    var viewController: UIViewController?

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesEnded(touches, with: event!)

    }
}

如果我能得到任何建议,那就太好了。提前致谢。

ios swift attributes uikit nsattributedstring
© www.soinside.com 2019 - 2024. All rights reserved.