What I am trying to do is press the speak button which will highlight the first paragraph and then speak that paragraph, then scroll the next paragraph to center of the textView and highlight that one and speak it and so own. What code would I have to add to do this?
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)
}
}