How to make attributed string within a UITextView accessible?

喜你入骨 提交于 2019-12-01 23:51:56

To make a portion of the NSMutableAttributedString selectable, you'll need to set it as a link like so:

quoteAttributedStr.setAsLink("string that you wish to be selectable goes here", linkURL: "url")

You'll also need to add the UITextViewDelegate, and add:

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
}

to handle it.

Here's an extension for the setAsLink that I forgot to include:

extension NSMutableAttributedString {

public func setAsLink(_ textToFind:String, linkURL:String) -> Bool {

    let foundRange = self.mutableString.range(of: textToFind)
    if foundRange.location != NSNotFound {
        self.addAttribute(NSLinkAttributeName, value: linkURL, range: foundRange)
        return true
    }
    return false
}
}

The problem deals with a specific VoiceOver gesture to be used when a link must be activated in a UITextView.

I created a blank project (iOS 12, Xcode 10) including the code snippet hereafter to get 2 URLs in the myTextView element :

class TextViewURLViewController: UIViewController, UITextViewDelegate {

    @IBOutlet weak var myTextView: UITextView!

    let myString = "Follow this developers guide if you already know the VoiceOver gestures."
    let myDevURL = "https://a11y-guidelines.orange.com/mobile_EN/dev-ios.html"
    let myGesturesURL = "https://a11y-guidelines.orange.com/mobile_EN/voiceover.html"


    override func viewDidLoad() {

        let attributedString = NSMutableAttributedString(string: myString)

        attributedString.addAttribute(.link,
                                      value: myDevURL,
                                      range: NSRange(location: 12,
                                                     length: 17))

        attributedString.addAttribute(.link,
                                      value: myGesturesURL,
                                      range: NSRange(location: 52,
                                                     length: 19))

        myTextView.attributedText = attributedString
        myTextView.font = UIFont(name: myTextView.font!.fontName,
                                 size: 25.0)
    }


    func textView(_ textView: UITextView,
                  shouldInteractWith URL: URL,
                  in characterRange: NSRange,
                  interaction: UITextItemInteraction) -> Bool {

        UIApplication.shared.open(URL, options: [:])
        return false
    }
}

Follow the steps hereunder to activate the link :

  1. Get the rotor links item with the appropriate gesture.
  2. Swipe up or down with one finger to reach the link.
  3. Double tap and hold until the event on step 4.

  1. A kind of popup shows up above the link.
  2. Once the action sheet appears, flick right to get the Open action.
  3. Double tap to open the URL and get the last screen on step 7.

Following the code snippet above, you can make attributed string within a UITextView accessible and open it by double tapping + holding until the popup appears above your link (this is how VoiceOver works).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!