作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想做的是按下朗读按钮,这将突出显示第一段,然后朗读该段落,然后将下一段滚动到 textView 的中心并突出显示该段落并朗读它等等。我必须添加什么代码才能做到这一点?
import UIKit
import AVFoundation
class TestViewController: UIViewController, AVSpeechSynthesizerDelegate {
let synthesizer = AVSpeechSynthesizer()
@IBOutlet weak var textViewOutlet: UITextView!
@IBAction func pauseSpeech(_ sender: Any) {
synthesizer.pauseSpeaking(at: AVSpeechBoundary.word)
}
@IBAction func stopSpeech(_ sender: Any) {
synthesizer.stopSpeaking(at: .word)
}
@IBAction func cancelButton(_ sender: Any) {
synthesizer.continueSpeaking()
}
@IBAction func speak(_ sender: AnyObject) {
let string = textViewOutlet.text!
let utterance = AVSpeechUtterance(string: string)
utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
synthesizer.speak(utterance)
}
override func viewDidLoad() {
super.viewDidLoad()
synthesizer.delegate = self
textViewOutlet.font = .systemFont(ofSize: 20)
}
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, willSpeakRangeOfSpeechString characterRange: NSRange, utterance: AVSpeechUtterance) {
let mutableAttributedString = NSMutableAttributedString(string: utterance.speechString)
mutableAttributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.blue, range: NSMakeRange(0, (utterance.speechString as NSString).length))
mutableAttributedString.addAttribute(.foregroundColor, value: UIColor.red, range: characterRange)
textViewOutlet.attributedText = mutableAttributedString
textViewOutlet.font = .systemFont(ofSize: 20)
}
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
textViewOutlet.attributedText = NSAttributedString(string: utterance.speechString)
textViewOutlet.font = .systemFont(ofSize: 20)
}
}
最佳答案
像这样:
定义属性
let currentParagraphIndex = 0
let paragraphs = [Range<String.Index>]()
开始时
let text = textViewOutlet.text!
paragraphs = splitOnParagraphs(text)
currentParagraphIndex = 0
highlightParagraph(paragraphs[currentParagraphIndex])
let utterance = AVSpeechUtterance(text.substringWithRange(paragraphs[currentParagraphIndex]))
utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
synthesizer.speak(utterance)
另见 Split paragraphs into Sentences
所以你开始说第一段,而不仅仅是文字。
然后,当段落结束时,在委托(delegate)中你开始说下一段:
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
currentParagraphIndex = currentParagraphIndex + 1
if (currentParagraphIndex < paragraphs.count) {
let utterance = AVSpeechUtterance(text.substringWithRange(paragraphs[currentParagraphIndex]))
highlightParagraph(currentParagraphIndex)
utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
synthesizer.speak(utterance)
}
}
关于swift - 如何突出显示每个段落并使用 speechSynthesizer 滚动 textView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49537860/
有没有一种方法可以“标记”对象的属性,使它们在反射中“突出”? 例如: class A { int aa, b; string s1, s2; public int AA
我是一名优秀的程序员,十分优秀!