gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:12:25 24 4
gpt4 key购买 nike

默认情况下,屏幕阅读器会选择整个 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

最佳答案

问题涉及 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. 操作表出现后,向右轻拂以获取打开 操作。
  3. 双击打开 URL 并获取第 7 步的最后一个屏幕。

description of the last three steps

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com