OGeek|极客世界-中国程序员成长平台

标题: ios - 如何使 UITextView 中的属性字符串可访问? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:23
标题: ios - 如何使 UITextView 中的属性字符串可访问?

默认情况下,屏幕阅读器将选择整个 View ,并且双击无法点击链接。事实上,在可访问性检查器上按下激活会使应用程序委托(delegate)崩溃,而控制台中没有任何堆栈跟踪。我已经尝试在 UITextView 本身中弄乱可访问性特征,但我没有任何运气。

let quoteAttributedStr = NSMutableAttributedString(string: "This is a test String" + " ")
                let enableLinkText = NSAttributedString(string: "this is the clickable text", attributes: dummyLinkAttribute)
                quoteAttributedStr.append(enableLinkText)

                return quoteAttributedStr

Example TextView



Best Answer-推荐答案


问题涉及 specific VoiceOver gesture当必须在 UITextView 中激活链接时使用。

我创建了一个空白项目 (iOS 12, Xcode 10),包括后面的代码片段,用于在 myTextView 元素中获取 2 个 URL:

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

按照以下步骤激活链接:

  1. 使用适当的手势获取转子 links 项。
  2. 用一根手指向上或向下滑动即可到达链接。
  3. 点按两次并按住直到第 4 步发生事件

description of the first three steps

  1. 链接上方会显示一种弹出窗口。
  2. 操作表出现后,向右滑动以获取 Open 操作。
  3. 点按两次以打开 URL 并获取第 7 步的最后一个屏幕。

description of the last three steps

按照上面的代码片段,您可以使 UITextView 中的属性字符串可访问,然后通过双击并按住直到弹出窗口出现在您的链接上方打开它(这就是 VoiceOver 的工作原理) .

关于ios - 如何使 UITextView 中的属性字符串可访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43121662/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4