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 }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter