SwiftUI中的Higlight语音发声

麦克·莫斯

我没有成功在SwiftUI中突出显示文本话语。我仅在UIKit中找到了它的示例。在UIKit中应该为var label: UILabel!,但在SwiftUI标签中必须为String。我试图转换NSMutableAttributedStringString内部函数格式,但是很抱怨。如何处理String格式,使其也可以在SwiftUI中使用?

import AVFoundation

class Speaker: NSObject {
    let synth = AVSpeechSynthesizer()
    var label: String // <- problem
    

    override init() {
        super.init()
        synth.delegate = self
    }

    func speak(_ string: String) {
        let utterance = AVSpeechUtterance(string: string)
        utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
        utterance.rate = 0.4
        synth.speak(utterance)
    }
    
    // Functions to highlight text
    func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, willSpeakRangeOfSpeechString characterRange: NSRange, utterance: AVSpeechUtterance) {
        let mutableAttributedString = NSMutableAttributedString(string: utterance.speechString)
        mutableAttributedString.addAttribute(.foregroundColor, value: UIColor.red, range: characterRange)
        label.attributedText = mutableAttributedString
    }

    func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
        label.attributedText = NSAttributedString(string: utterance.speechString)
    }
}
jnpdx

我建议您进行包装UILabelUIViewRepresentable以便可以像以前一样继续使用属性字符串:


struct ContentView : View {
    @ObservedObject var speaker = Speaker()
    
    var body: some View {
        VStack {
            LabelRepresented(text: speaker.label)
        }.onAppear {
            speaker.speak("Hi. This is a test.")
        }
    }
}

struct LabelRepresented: UIViewRepresentable {
    var text : NSAttributedString?
    
    func makeUIView(context: Context) -> UILabel {
        return UILabel()
    }
    
    func updateUIView(_ uiView: UILabel, context: Context) {
        uiView.attributedText = text
    }
}

class Speaker: NSObject, ObservableObject, AVSpeechSynthesizerDelegate {
    let synth = AVSpeechSynthesizer()
    @Published var label: NSAttributedString? // <- change to AttributedString
    

    override init() {
        super.init()
        synth.delegate = self
    }

    func speak(_ string: String) {
        let utterance = AVSpeechUtterance(string: string)
        utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
        utterance.rate = 0.4
        synth.speak(utterance)
    }
    
    // Functions to highlight text
    func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, willSpeakRangeOfSpeechString characterRange: NSRange, utterance: AVSpeechUtterance) {
        let mutableAttributedString = NSMutableAttributedString(string: utterance.speechString)
        mutableAttributedString.addAttribute(.foregroundColor, value: UIColor.red, range: characterRange)
        label = mutableAttributedString
    }

    func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
        label = NSAttributedString(string: utterance.speechString)
    }
}

我将其更改label为,NSAttributedString?并将其设置为@Published属性ObservableObject,这样,当更改时,视图会立即得到通知。

UIViewRepresentable创建一个标签,并用属性串任何它改变时更新它。

如果您确实想尝试一种更纯粹的SwiftUI方法,此博客文章提供了一些在SwiftUI中使用NSAttributedString的良好资源(包括我采用的方法):https ://swiftui-lab.com/attributed-strings-with -swiftui /

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

静音模式下的AVSpeechUtterance发声语音

来自分类Dev

Web语音合成:暂停发声1,播放另一发声2,然后恢复发声1-可能吗?

来自分类Dev

语音中的几个语音命令

来自分类Dev

网站中的语音字体

来自分类Dev

语音识别中的MFCC

来自分类Dev

UbuntoTouch中的语音加密

来自分类Dev

iOS 中的蓝牙语音

来自分类Dev

TTS Swift 中的语音

来自分类Dev

在C ++中从C转发声明匿名结构

来自分类Dev

模板类/函数中的交叉转发声明

来自分类Dev

从转发声明的类的函数中获取返回类型

来自分类Dev

Tkinter中的python语音识别

来自分类Dev

Windows IOT中的语音语言

来自分类Dev

在Gmail中语音转文字?

来自分类Dev

C#中的语音合成

来自分类Dev

语音AI中的if语句嵌套

来自分类Dev

GoldenDict中的文字转语音

来自分类Dev

vim:python三引号字符串中的higlight SQL

来自分类Dev

vim:python三引号字符串中的higlight SQL

来自分类Dev

C#从语音标记中获取语音

来自分类Dev

获取语音合成中的语音列表(Web Speech API)

来自分类Dev

如何在javascript中修复语音合成语音

来自分类Dev

JavaScript中是否有语音或语音就绪事件?

来自分类Dev

语音API OneCore中的Sayaka语音在哪里?

来自分类Dev

ezplot上的Higlight间隔

来自分类Dev

从清单中删除不在“语音”中的项目?

来自分类Dev

Google Glass:活动中的语音命令

来自分类Dev

可以离线工作的android中的语音识别

来自分类Dev

限制C#中的语音识别选择