SwiftUI - 如何为 VoiceOver 添加属性可访问性标签,以控制语音间距?

SwiftUI - How to add attributed accessibility label for VoiceOver, to control spoken pitch?

我想让 VoiceOver 在多个音高中说出 View 的辅助功能标签。例如,对于 “Raised string. Normal string.”,我希望“Raised string”具有 1.5 音高,而“Normal string”。具有默认的 1.0 间距。

使用 UIKit,我可以用 NSAttributedStringNSAttributedString.Key.accessibilitySpeechPitch 设置元素的 accessibilityAttributedLabel。像这样:

let pitchAttribute = [NSAttributedString.Key.accessibilitySpeechPitch: 1.5]
let string = NSMutableAttributedString()

let raisedString = NSAttributedString(string: "Raised string.", attributes: pitchAttribute)
string.append(raisedString)

let normalString = NSAttributedString(string: "Normal string.")
string.append(normalString)

squareView.isAccessibilityElement = true
squareView.accessibilityAttributedLabel = string

结果,正是我想要的(Audio link):

然而,对于 SwiftUI,似乎只有一个 .accessibility(label: Text) 修饰符。这是我的代码:

struct ContentView: View {
    var body: some View {
        Rectangle()
            .fill(Color.blue)
            .frame(width: 100, height: 100)
            .accessibility(label: Text("Raised string. Normal string."))
    }
}

这是结果 (Audio link):

正如您所听到的,“拉高琴弦。”和“普通字符串”。以相同的音高说话。这符合预期,因为我为标签传入了一个单独的 Text

但是有什么方法可以在 SwiftUI 中设置语音音高吗?我找不到一种方法来设置一个音高,更不用说两个了。

嗯,我想 UIViewRepresentable 时间到了(耶)。除非有人有更好的答案,否则这就是我想出的:

struct ContentView: View {
    var body: some View {
        RectangleView(
            accessibilityAttributedLabel: getAccessibilityAttributedLabel(), 
            fill: UIColor.blue /// pass color into initializer
        )
        .frame(width: 100, height: 100)
    }
    
    /// make the attributed string
    func getAccessibilityAttributedLabel() -> NSAttributedString {
        let pitchAttribute = [NSAttributedString.Key.accessibilitySpeechPitch: 1.5]
        let string = NSMutableAttributedString()

        let raisedString = NSAttributedString(string: "Raised string.", attributes: pitchAttribute)
        string.append(raisedString)

        let normalString = NSAttributedString(string: "Normal string.")
        string.append(normalString)
        
        return string
    }
}

struct RectangleView: UIViewRepresentable {
    var accessibilityAttributedLabel: NSAttributedString
    var fill: UIColor
    
    func makeUIView(context: UIViewRepresentableContext<Self>) -> UIView {
        let uiView = UIView()
        uiView.backgroundColor = fill
        uiView.isAccessibilityElement = true
        uiView.accessibilityAttributedLabel = accessibilityAttributedLabel /// set the attributed label here
        
        return uiView
    }
    
    func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<Self>) {}
}

很高兴看到开发人员致力于包括辅助功能!

在 iOS 15 中,您可以让函数 return 成为 AttributedString,然后使用函数中的 AttributedString 设置视图的辅助功能标签:

.accessibility(label: Text(getAccessibilityAttributedLabel()))

具有一个音高值的示例函数:

func getAccessibilityAttributedLabel() -> AttributedString {
    var pitchSpeech = AttributedString("Raised pitch")
    pitchSpeech.accessibilitySpeechAdjustedPitch = 1.5
    return pitchSpeech
}