In my Swift program, I use AVSpeechSynthesizer to pronounce Chinese characters. This is my method:
static var synth = AVSpeechSynthesizer()
static func speak(string: String)
{
let utterance = AVSpeechUtterance(string: string)
utterance.voice = AVSpeechSynthesisVoice(language: "zh-CN")
utterance.rate = 0.1
synth.speak(utterance)
}
Often, a single character can have multiple pronunciations. Is there a way to use AVSpeechSynthesizer to pronounce a character given the pinyin. In other words, instead of passing the character "高", we would pass the pinyin "gāo". So far, just passing the pinyin has not worked in this method.
Many thanks in advance,
Jon
I think I've found the answer, and am posting it in case anybody else might find it useful. The pinyin should be entered in the form "gao1", not "gāo". It appears that it must be lower-case: i.e., "GAO1" doesn't seem to work, but "gao1" does.
In my view, using the IPA notation (International Phonetic Alphabet) is the best way to reach your goal.
Put an attributedString as incoming parameter of the AVSpeechUtterance instance after declaring its appropriate attribute.
Take a look at this WWDC 2018 video detailed summary that should help to use AVSpeechSynthesizer for Chinese pinyin pronunciation : helpful implementation can be found in this answer as well.
Related
I need to read some metadata which can contain French or German characters & accents.
I think that I need to read the metadata as UTF8 String. But being a beginner in Swift and Apple in general I don't know how to do this.
I tried to implement some similar solutions found on stackOverflow but none worked.
What I'm working with:
let firstMeta: MPTimedMetadata = radioPlayer.timedMetadata.first as! MPTimedMetadata
let metaData = firstMeta.value as! String
print(metaData)
There I get
Antonin DvoÅák
instead of
Antonin Dvořák
Any ideas?
Easy solution for how to get UTF8 metadata from online audio stream using AVPlayer
https://stackoverflow.com/a/37831097/4249825
This is a problem in the library, you cannot fix it because the string is already decoded from data.
There is a reason why the whole set of classes was deprecated in iOS 9.
You should use AVPlayerViewController & AVPlayer & AVPlayerItem & AVMetadataItem.
They are solving this issue. Don't use MPMoviePlayerController.
let convertedString = String(data: metaData.dataUsingEncoding(NSISOLatin1StringEncoding)!, encoding: NSUTF8StringEncoding)!
I'm taking the Stanford iPad and iPhone developer course online at Stanford using Swift and working on creating a Calculator application. (Still a bit new to programming.)
However, it keeps crashing with the error message whenever I select an operator from the calculator.
operandStack = [36.0]
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
and then when I look in ViewController to see what part of the source code is highlighted. It is this:
NSNumberFormatter().numberFromString(display.text!)!.doubleValue }
the error message under this though, reads:
THREAD 1:EXC_BAD_INSTRUCTION(code:EXC_1386_INVOP,subcode=0x0)
I don't understand what's going on, since I entered the code, word for word from the lecture.
Any help is appreciated!
Thanks in advance!
I am stuck at that part of the course too!
I think that apple recently made changes to NSNumberFormatter's numberFromString method. Because when I printed out display.text!, there was nothing wrong. In other words, it does not found nil while unwrapping that part.
Another part we are unwrapping is here, at the second ! mark, we unwrap only this part:
NSNumberFormatter().numberFromString(display.text!)
But we have an error out of this, so numberFromString should be returning nil.
But in the videos, it doesn't. It perfectly turns floating point number strings (such as "36.0") to NSNumber, then to Double.
And since your question was asked on May 20th and I could not find any "old" questions, I think Apple had changed the code on numberFromString.
Edit: I did something crazy and used Find & Replace (command + F) to replace all "Double"s to "Int"s in my code. The multiplication part works well now, I think the problem is about the "." part on Doubles.
Edit 2: I solved it. Some countries such as US use "." to separate decimals and some others such as Turkey use "," to do it. It works on video because he's doing it on US.
NSNumberFormatter has a property called decimalSeparator. We have to set it to ".". I did the following changed to my code and it worked perfect.
var displayValue: Double {
get {
var formatter = NSNumberFormatter()
formatter.decimalSeparator = "."
return (formatter.numberFromString(display.text!)!.doubleValue)
}
set {
display.text = "\(newValue)"
userIsInTheMiddleOfTypingNumber = false
}
}
"found nil while unwrapping an Optional" means that you have a variable that may or maynot have a value, when you use the operator ! you are telling swift "Trust me there is a value in there" however if there is not swift will throw a exception as the one you just saw.
The best way to avoid this is checking before use:
if let value = display.text{
//if your code get here value is safe to use
NSNumberFormatter().numberFromString(value).doubleValue
}
I'm trying to find solution for it and confused how do I display third power/ cube in a UILabel. I tried to find answer in previously asked question but none of them were useful.
Questions i tried to get answers :
how-to-show-superscript-for-registered-symbol
UILabel and superscript
If I have to use unichars how do I use them for Superscripts??
Thanks..
You need to include the unicode symbol for a superscripted three:
NSInteger number = 10;
NSString *cubedSymbol = #"\u00B3";
NSString *tenCubed = [NSString stringWithFormat:#"%d%#",number,cubedSymbol];
Plenty more fun is to be had here
Another solution would be to use the Core Text framework to draw an NSAttributedString with the kCTSuperscriptAttributeName attribute on the section of the string you want to make superscript. This ends up being more work including custom drawing and things, but is more flexible than relying on unicode characters.
Here’s a blog post I found with some more information: http://iphonedevelopment.blogspot.com/2011/03/attributed-strings-in-ios.html
NSAttributedString on Mac OS X has lots of nice uses built into AppKit, but Apple hasn’t made it easy with UIKit for iOS.
I've localized an app for the iPhone. No surprise, the localization includes some accents:
"Touch cards to select. Then touch
'Bid'." = "Touchez les cartes pour les
sélectionner, puis touchez 'Miser'.";
These work fine in high-level stuff, like when I put the text into a table, but when I try to write them to a UIView by hand, the accents get mangled:
I'm using kCGEncodingMacRoman and UTF8, both of which should support accents, I think, but I'm clearly missing something:
CGContextSelectFont(ctx,fontName,thisWriting.fontSize,kCGEncodingMacRoman);
CGContextShowTextAtPoint(ctx,
thisWriting.center.x - floor(thisTextSize.width/2),
yThusFar,
[thisText UTF8String],
[thisText length]);
The font is some variant of ArialMT. thisText is just an NSString.
Quartz provides a limited, low-level interface for drawing text. For information on text-drawing functions, see CGContext Reference. For full Unicode and text-layout support, use the services provided by Core Text or ATSUI).
To expand on what sorin said: Quartz/Core Graphics do not support Unicode text, which includes the accents you need for foreign languages. There have traditionally been a number of alternative ways to resolve this, but currently the best answer is to use Core Text, which can write directly to a graphical context, as I was doing here.
The main element in Core Text is the NSAttributedString or NSMutableAttributed String class.
Here's similar code to what I had for Core Text:
CTLineRef thisLine = CTLineCreateWithAttributedString((CFAttributedStringRef)thisAText);
CGContextSetTextPosition(ctx, self.center.x - floor(thisTextSize.width/2), yThusFar);
CTLineDraw(thisLine, ctx);
The font is already taken care of, because it's part of that NSAttributedString (or CFAttributedStringRef, which is a toll-free bridged equivalent).
Here's the result:
i am using uiwebview in my application. there are some links when user clicks a http search starts. it works fine but i have problems while getting "%58 den ysnky'ye tepki" it is given as "X'den ysnky'ye tepki". it has problems with % char.
identifier:%58'den%20ysnky'ye%20tepki
decoded identifier:X'den ysnky'ye tepki
i am using stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding to decode the string like that;
NSLog(#"identifier:%#", identifier);
identifier = [identifier stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"decoded identifier:%#", identifier);
how can i get the correct string?
thanks...
It looks like your string may not be encoded properly in the first place. %58 is the correct encoding for the letter “X” (see this ASCII table). As far as I can tell, therefore, the decode is behaving properly.
What are you expecting?