将可访问性标识符添加到NSMutableAbeatTribedString的子字符串中

发布于 2025-02-05 20:32:35 字数 1989 浏览 3 评论 0原文

我正在尝试在uilabel's 属于属性属性上配置nsmutableAbeatTributterString '

目标

  • uilabel带有nsattributtedString具有单击的链接,该链接打开URL,并且
  • 在链接上具有appium/voveover可以找到的accessiencyIdendifier

现在正在发生的事情

,链接按预期显示,但是当我使用可访问性检查员查看它们时,它将它们视为一个元素,但是我可以单击链接并按预期打开URL。这是我用来添加链接的扩展程序的实现:

extension NSMutableAttributedString {
    @discardableResult
    public func setLink(for text: String,
                        linkURL: String) -> Bool {
        guard let url = URL(string: linkURL) else { return false }
        let linkRange = self.mutableString.range(of: text)

        guard linkRange.location != NSNotFound else { return false }

        self.addAttribute(.link,
                          value: url,
                          range: linkRange)

        // This doesn't do anything :(
        self.addAttribute(.accessibilityTextCustom,
                          value: text,
                          range: linkRange)
        
        return true
    }
}

用法:

    private lazy var attributedString: NSMutableAttributedString = {
        let string = "CNN and ESPN"
        let attributedString = NSMutableAttributedString(string: string)

        attributedString.setLink(for: "CNN", linkURL: "https://www.cnn.com")
        attributedString.setLink(for: "ESPN", linkURL: "https://www.espn.com")

        return attributedString
    }()

    myLabel.attributedText = attributedString

浏览文档,我看到 AccessibilityLink ,似乎可以完成工作,但它是MacOS 10.4+,而不是iOS。关于如何在appium/convainible Inspector中显示的nsmutableAttributterString的子字符串上如何获得可访问性标识符的想法?

如果这不是要走的路,是否有办法使用可访问性容器来执行此操作?

I'm attempting to configure a NSMutableAttributedString on a UILabel's attributedText property to display accessibility identifiers for links within the attributed string.

Objective

  • UILabel with NSAttributedString that's got a clickable link which opens a URL and...
  • Has an accessibilityIdentifier on the link that Appium/VoiceOver can find.

What's Happening Now

Right now, the links display as expected, but when I view them with the Accessibility Inspector, it sees them as a single element, however I can click the links and they open the URL as expected. This is the implementation for the extension I'm using to add a link:

extension NSMutableAttributedString {
    @discardableResult
    public func setLink(for text: String,
                        linkURL: String) -> Bool {
        guard let url = URL(string: linkURL) else { return false }
        let linkRange = self.mutableString.range(of: text)

        guard linkRange.location != NSNotFound else { return false }

        self.addAttribute(.link,
                          value: url,
                          range: linkRange)

        // This doesn't do anything :(
        self.addAttribute(.accessibilityTextCustom,
                          value: text,
                          range: linkRange)
        
        return true
    }
}

Usage:

    private lazy var attributedString: NSMutableAttributedString = {
        let string = "CNN and ESPN"
        let attributedString = NSMutableAttributedString(string: string)

        attributedString.setLink(for: "CNN", linkURL: "https://www.cnn.com")
        attributedString.setLink(for: "ESPN", linkURL: "https://www.espn.com")

        return attributedString
    }()

    myLabel.attributedText = attributedString

Looking through documentation, I see accessibilityLink, which seems like it would get the job done, but it's for MacOS 10.4+, not iOS. Any thoughts on how to get an accessibility identifier on a substring of an NSMutableAttributedString that shows up in Appium/Accessibility Inspector?

If that's not the way to go, is there a way to do this with an accessibility container?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文