Making VocalTextEdit… Vocal

Logging the text is nice, but the real goal of VocalTextEdit is that the computer will read your users’ text to them. Cocoa provides a class for synthesizing speech, suitably named NSSpeechSynthesizer. Begin by adding a property to your ViewController that is an instance of NSSpeechSynthesizer.

Listing 26.4  Adding an instance of NSSpeechSynthesizer (ViewController.swift)

class ViewController: NSViewController {

    let speechSynthesizer = NSSpeechSynthesizer()

    @IBOutlet var textView: NSTextView!

    @IBAction func speakButtonClicked(sender: NSButton) {
        print("I should speak \(textView.string)")
    }

    @IBAction func stopButtonClicked(sender: NSButton) {
        print("The stop button was clicked")
    }

}

The default ...

Get Swift Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.