NSNumberFormatter:用AND粒子拼写数字(英文拼写)

时间:2014-04-07 19:20:16

标签: ios objective-c nsnumberformatter avspeechsynthesizer

以下代码:

AVSpeechSynthesizer * speechSynthesizer = [[AVSpeechSynthesizer alloc] init];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString: @"112"];
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-GB"];
speechSynthesizer  speakUtterance:utterance];

导致设备说:"一百十二" (英国拼写)

但是,如果您改为音译112:

NSString * wordNumber = nil;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en-GB"]];
[numberFormatter setNumberStyle:NSNumberFormatterSpellOutStyle];
wordNumber = [numberFormatter stringFromNumber:@(112)];

现在wordNumber包含"一百一十二" (没有和粒子)。

所以:

@"112" -> AVSpeechSynthesizer -> "one hundred and twelve"
@"112" -> NSNumberFormatter   -> "one hundred twelve"

如何用粒子音译数字,即英国拼写?

2 个答案:

答案 0 :(得分:1)

我相信你自己需要这样做。我无法产生您想要的结果或找到任何允许我这样做的资源。

我已经链接到一个演示如何手动执行此操作的问题。

Is it possible to spell out numbers in words with a separator?

答案 1 :(得分:0)

嗯,你不能这样做吗?

AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"112"];
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-GB"];
NSString *transliteration = utterance.speechString;