Terms and Conditions label clickable also handling check btn click. check btn images are from sf Symbols

PHOTO EMBED

Tue Oct 10 2023 06:06:48 GMT+0000 (Coordinated Universal Time)

Saved by @hasnat #ios #swift #label #terms #conditions #termsandconditions

    override func viewDidLoad() {
        super.viewDidLoad()
      
      setTermsAndConditionsLabel()
    }

  @IBAction func checkButtonPressed(_ sender: Any) {
        if check {
            checkImageView.isHidden = true
            uncheckImageView.isHidden = false
            check = false
        } else {
            checkImageView.isHidden = false
            uncheckImageView.isHidden = true
            check = true
        }
    }

@objc func tappedTermsAndConditions(_ sender: UITapGestureRecognizer) {
        if let link = URL(string: "https://www.google.com") {
            UIApplication.shared.open(link)
        }
    }
    
    func setTermsAndConditionsLabel() {
        
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tappedTermsAndConditions))
        termsAndConditionsLabel.isUserInteractionEnabled = true
        termsAndConditionsLabel.addGestureRecognizer(tapGestureRecognizer)
        
        let fullText = "I agree to the Terms and Conditions"
        let attributedString = NSMutableAttributedString(string: fullText)

        let grayAttributes: [NSAttributedString.Key: Any] = [
            .foregroundColor: UIColor.gray,
        ]
        
        let boldAttributes: [NSAttributedString.Key: Any] = [
            .font: UIFont.boldSystemFont(ofSize: 17), // Replace with your desired font size
        ]

        let blueAttributes: [NSAttributedString.Key: Any] = [
            .foregroundColor: hexStringToUIColor(hex: "2563EB"),
            .underlineStyle: NSUnderlineStyle.single.rawValue,
            .link: "termsAndConditionsURL", // Replace with your actual URL
        ]

        let grayRange = (fullText as NSString).range(of: "I agree to the")
        attributedString.addAttributes(grayAttributes, range: grayRange)

        let blueRange = (fullText as NSString).range(of: "Terms and Conditions")
        attributedString.addAttributes(blueAttributes, range: blueRange)
        
        let boldRange = (fullText as NSString).range(of: "Terms and Conditions")
        attributedString.addAttributes(boldAttributes, range: boldRange)

        termsAndConditionsLabel.attributedText = attributedString
    }
content_copyCOPY