Text To Speech In iPhone

The iOS 7 operating system was a great advance for Apple mobile devices, and iOS 8 promises to be even more impressive. With the vast number of features added to iOS 7 last fall, it is easy to overlook a few of them. In this blog, I’ll show you how to use the speech synthesizer to convert text to audible speech. The full source code can be found here: DictationTest (Note that this code was part of a larger dictation proof of concept, hence the name)

The speech synthesizer is a part of the AVFoundation framework, so the first thing we need to do is link against that framework in our app, and import AVFoundation/AVFoundation.h. We’ve done this in the ViewController.h file for simplicity:

We’ve also added a property for a text view, and an action method for dismissing the text view’s keyboard. In this method, we’ll convert the text to audio, and play it using the speech synthesizer as well.

The implementation of the dismissKeyboard method in ViewController.m is listed here:


First, we resign the first responder on the text view. The first responder is the keyboard; resigning it removes it from the UI. After doing this, we instantiate an AVSpeechSynthesizer and an AVSpeechUtterance. The synthesizer will do the actual speeking, and the utterance is the text we want to have spoken, converted to a format suitable for the synthesizer.

All that remains is to speak the text; we do this with the speakUtterance method of the synthesizer. But before we do this, we have an opportunity to set a couple of properties on the utterance object to control how the speech will sound. The rate property controls how fast the text will be spoken. The value is a float ranging from AVSpeechUtteranceMinimumSpeechRate to AVSpeechUtteranceMaximumSpeechRate, with a third constant defined at the default speech rate (AVSpeechUtteranceDefaultSpeechRate). The higher the speech rate, the faster the text will be spoken.

The pitchMultiplier controls the pitch (high or low) of the voice. Again, this is a float, this time in the range 0.5 to 2.0. The default value is 1.0, of course.

The user interface for the app is very simple: just a text view, a label, and a button. Wire up the text view to the UITextView outlet, and the dismissKeyboard method to the “Speak Entered Text” button’s touchUpInside event. I’ve changed the background color of the text view to make it stand out a bit. Have fun!


Leave a Comment: