菜鸟教程小白 发表于 2022-12-11 19:23:22

ios - 如何使 UITextView 中的属性字符串可访问?


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

<pre><code>let quoteAttributedStr = NSMutableAttributedString(string: &#34;This is a test String&#34; + &#34; &#34;)
                let enableLinkText = NSAttributedString(string: &#34;this is the clickable text&#34;, attributes: dummyLinkAttribute)
                quoteAttributedStr.append(enableLinkText)

                return quoteAttributedStr
</code></pre>

<p> <a href="/image/Kzf5Y.png" rel="noreferrer noopener nofollow"><img src="/image/Kzf5Y.png" alt="Example TextView"/></a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>问题涉及 <a href="http://a11y-guidelines.orange.com/mobile_EN/voiceover.html#OpenLink" rel="noreferrer noopener nofollow"><strong>specific VoiceOver gesture</strong></a>当必须在 UITextView 中激活链接时使用。</p>

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

<pre><code>class TextViewURLViewController: UIViewController, UITextViewDelegate {

    @IBOutlet weak var myTextView: UITextView!

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


    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) -&gt; Bool {

      UIApplication.shared.open(URL, options: [:])
      return false
    }
}
</code></pre>

<p>按照以下步骤激活链接:</p>

<ol>
<li>使用适当的手势获取转子 <code>links</code> 项。</li>
<li>用一根手指向上或向下滑动即可到达链接。</li>
<li>点按两次并<strong>按住直到第 4 步发生事件</strong>。</li>
</ol>

<p> <a href="/image/yBeOs.png" rel="noreferrer noopener nofollow"><img src="/image/yBeOs.png" alt="description of the first three steps"/></a> </p>

<ol start="4">
<li>链接上方会显示一种弹出窗口。</li>
<li>操作表出现后,向右滑动以获取 <code>Open</code> 操作。</li>
<li>点按两次以打开 URL 并获取第 7 步的最后一个屏幕。</li>
</ol>

<p> <a href="/image/KnOIA.png" rel="noreferrer noopener nofollow"><img src="/image/KnOIA.png" alt="description of the last three steps"/></a> </p>

<p>按照上面的代码片段,您可以<strong>使 UITextView 中的属性字符串可访问</strong>,然后通过双击并按住直到弹出窗口出现在您的链接上方打开它<em>(这就是 VoiceOver 的工作原理) </em>.</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何使 UITextView 中的属性字符串可访问?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/43121662/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/43121662/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何使 UITextView 中的属性字符串可访问?