如何在Mac应用程序中说出文字?

use*_*348 -1 macos cocoa objective-c

我正在制作一个Xcode应用程序,我需要能够按下按钮并阅读一些文本.如果有人可以向我提供我需要在IBAction按钮的括号内使用的代码行,那将非常感激.谢谢.

Ano*_*dya 7

试试这个:

你需要一个房产

@property(strong) NSSpeechSynthesizer *speechSynth;
Run Code Online (Sandbox Code Playgroud)

在你的init方法中:

_speechSynth = [[NSSpeechSynthesizer alloc] initWithVoice:nil];
Run Code Online (Sandbox Code Playgroud)

开始发言:

- (IBAction)sayIt:(id)sender{
    NSString *string = [self.textField stringValue];
    // Is the string zero-length?
    if ([string length] == 0) {
        NSLog(@"string from %@ is of zero-length", self.textField);
        return;
    }
    [self.speechSynth startSpeakingString:string];
    NSLog(@"Have started to say: %@", string);
}
Run Code Online (Sandbox Code Playgroud)

停止说话:

- (IBAction)stopIt:(id)sender {
    NSLog(@"stopping");
    [self.speechSynth stopSpeaking];
}
Run Code Online (Sandbox Code Playgroud)