Use CFStringTransform to convert Chinese characters to PinYin (excluding tones) and cfstringtransform
Usage:
NSMutableString * source = [sourceString mutableCopy];
CFStringTransform (_ bridge CFMutableStringRef) source, NULL, kCFStringTransformMandarinLatin, NO );
CFStringTransform (_ bridge CFMutableStringRef) source, NULL, kCFStringTransformStripDiacritics, NO );
Return source;
In this method, kCFStringTransformMandarinLatin converts Chinese characters to pinyin, and then calls kCFStringTransformStripDiacritics to remove the tone,
The mistake I made here was not to write both lines, but to write only one line, so it would have been the first line, and only the pinyin with tone would have been transferred, only write the second row and only return Chinese characters,
Later, my leadership optimized the method, because some words will be converted into incorrect pinyin, so the complete method is as follows and sorting is supported,
-(NSString *) phonetic :( NSString *) sourceString {
If ([sourceString isEqualToString: @ ""]) {
Return sourceString;
}
NSMutableString * source = [sourceString mutableCopy];
CFStringTransform (_ bridge CFMutableStringRef) source, NULL, kCFStringTransformMandarinLatin, NO );
CFStringTransform (_ bridge CFMutableStringRef) source, NULL, kCFStringTransformStripDiacritics, NO );
If ([[(NSString *) sourceString substringToIndex: 1] compare: @ "long"] = NSOrderedSame)
{
[Source replaceCharactersInRange: NSMakeRange (0, 5) withString: @ "chang"];
}
If ([[(NSString *) sourceString substringToIndex: 1] compare: @ "shen"] = NSOrderedSame)
{
[Source replaceCharactersInRange: NSMakeRange (0, 4) withString: @ "shen"];
}
If ([[(NSString *) sourceString substringToIndex: 1] compare: @ ""] = NSOrderedSame)
{
[Source replaceCharactersInRange: NSMakeRange (0, 3) withString: @ "xia"];
}
If ([[(NSString *) sourceString substringToIndex: 1] compare: @ ""] = NSOrderedSame)
{
[Source replaceCharactersInRange: NSMakeRange (0, 3) withString: @ "di"];
}
If ([[(NSString *) sourceString substringToIndex: 1] compare: @ "weight"] = NSOrderedSame)
{
[Source replaceCharactersInRange: NSMakeRange (0, 5) withString: @ "chong"];
}
Return source;
}
How can we combine the tones of Chinese characters and pinyin?
... I doubt if you are Chinese.
I would like to express my gratitude to the software for converting Chinese characters to pinyin, which can be used as a Chinese character or for converting Chinese characters online.
.
Hello!
The following address can be converted, and multiple words can be converted together. Hope to help you!
Hanyu.iciba.com/pinyin
Have a good time!
.