Skip to content

Instantly share code, notes, and snippets.

// View all the voices
NSLog(@"%@", [AVSpeechSynthesisVoice speechVoices]);
/* Output
th-TH pt-BR sk-SK
fr-CA ro-RO no-NO
fi-FI pl-PL de-DE
nl-NL id-ID tr-TR
it-IT pt-PT fr-FR
ru-RU es-MX zh-HK
/* If no voice is specified, the system's default will be used. */
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
/* Setting these values after a speech utterance has been enqueued will have no effect. */
utterance.rate = AVSpeechUtteranceMaximumSpeechRate/2; // Values are pinned between AVSpeechUtteranceMinimumSpeechRate and AVSpeechUtteranceMaximumSpeechRate.
utterance.pitchMultiplier = 1.5; // [0.5 - 2] Default = 1
utterance.volume = 0.5; // [0-1] Default = 1
utterance.preUtteranceDelay = 1; // NSTimeInterval Default is 0.0
utterance.postUtteranceDelay = 1; // NSTimeInterval Default is 0.0
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance
{
// Highlight the portion of text that will be spoken.
NSMutableAttributedString *aString = [[NSMutableAttributedString alloc] initWithString:_textLabel.text];
[aString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:characterRange];
_textLabel.attributedText = aString;
}
// Create a synth
AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc] init];
//create an utterance
AVSpeechUtterance* utterance = [AVSpeechUtterance speechUtteranceWithString:@"Hello World"];
//Speak!
[synth speakUtterance:utterance];
@baphillips
baphillips / NSBundle-iOS7-Fix.m
Last active January 6, 2016 15:32
In iOS 7 NSBundle pathForResource:ofType:inDirectory: continued to return nil. It is believed that this is a bug in the SDK. Here is what I used in the meantime.
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *filePath = [bundlePath stringByAppendingPathComponent:@"image.png"];
@baphillips
baphillips / iOS-7-boundingRectWithSize-options-attributes-context.m
Last active July 26, 2018 15:26
iOS 7 dynamic UILabel frame adjustment for a given NSString and UIFont using boundingRectWithSize:options:attributes:context:.
NSString* string = @"Hello World";
UIFont *font = [UIFont fontWithName:@"Helvetica-BoldOblique" size:21];
CGSize constraint = CGSizeMake(300,NSUIntegerMax);
NSDictionary *attributes = @{NSFontAttributeName: font};
CGRect rect = [string boundingRectWithSize:constraint
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)