-
-
Save dadederk/10990d98f25afa994985234223b3c6a1 to your computer and use it in GitHub Desktop.
UITextView to implement a block of text with links in it using attributed strings so they're accessible. #365DaysIOSAccessibility. Day 30.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let textView = UITextView() | |
| let string = "I agree with the Privacy Policy and the Terms and Conditions" | |
| let attributedString = NSMutableAttributedString(string: string) | |
| attributedString.addAttribute(.link, | |
| value: "https://www.yourdomain.com/pp", | |
| range: NSRange(location: 17, length: 14)) | |
| attributedString.addAttribute(.link, | |
| value: "https://www.yourdomain.com/tac", | |
| range: NSRange(location: 40, length: 20)) | |
| textView.attributedText = attributedString | |
| extension ViewController: UITextViewDelegate { | |
| func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool { | |
| UIApplication.shared.open(URL) | |
| return true | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment