swift - 在 SwiftUI 中突出显示语音
问题描述
我没有成功在 SwiftUI 中突出显示文本。我只在 UIKit 中找到了它的例子。在 UIKit 中它应该是var label: UILabel!
,但在 SwiftUI 中标签必须是 String。我试图转换NSMutableAttributedString
成String
格式,内部函数,但它在抱怨。如何使用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)
}
}
解决方案
我建议您换行UILabel
,UIViewRepresentable
以便您可以像以前一样继续使用属性字符串:
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
为 anNSAttributedString?
并将其设置为 an 上的 @Published 属性ObservableObject
——这样,当它更改时,视图会立即得到通知。
UIViewRepresentable
创建一个标签,并在其更改时使用属性字符串对其进行更新。
如果您确实想尝试更纯粹的 SwiftUI 方法,这篇博文有一些在 SwiftUI 中使用 NSAttributedString 的好资源(包括我采用的方法):https ://swiftui-lab.com/attributed-strings-with -swiftui/
推荐阅读
- c++ - 点光源随相机移动
- html - 跨浏览器错误(我尝试了 CSS 重置)
- python - 字典键值仅显示唯一结果而不是全部
- r - 在ggplot2中将空类添加到直方图中
- google-apps-script - 谷歌金融作为 ArrayFormula 的论据
- performance - Web 视图中的 Web URL 加载时间反应原生 - 如何检查?
- parity - 如何使用 Parity 基板创建任意数据的哈希
- processing - 尝试实现广度优先搜索可视化器需要帮助动画过程
- django - 为什么这个中间 Django 页面不会回到之前的操作?
- red-black-tree - 红黑树高证明