In iOS development often touch the need to convert Chinese characters into Pinyin, the following is the way I convert Chinese characters to pinyin
Code implementation
+ (NSString*) Transform: (NSString*) chinese{//Replace the nsstring with nsmutablestring nsmutablestring*pinyin = [Chinese mutablecopy];//convert Chinese characters to pinyin (bands standard)Cfstringtransform (__bridge cfmutablestringref) Pinyin,NULL, Kcfstringtransformmandarinlatin,NO);NSLog(@"%@", pinyin);//Remove phonetic transcription of pinyinCfstringtransform (__bridge cfmutablestringref) Pinyin,NULL, Kcfstringtransformstripcombiningmarks,NO);NSLog(@"%@", pinyin);//Return recent results returnPinyin;}
With kCFStringTransformMandarinLatin
The method converts the pinyin of the bands, and if you need to remove the phonetic transcription, continue to use kCFStringTransformStripCombiningMarks
Method can be used.
Method Introduction
In iOS to achieve the above functions, in fact, the main method is a CFStringTransform great effort, we will focus on this method
//函数原型Booleanstring, CFRange *rangeBoolean reverse);
Parameters specific Description
- String: Strings that need to be converted. Since this parameter is a cfmutablestringref type, a nsmutablestring type can also be passed in as a free bridging method.
- Range: The scope in which the conversion operation works. This parameter is Cfrange, not nsrange. when given null, the operation scope is all .
- Transform: Transforms that need to be applied. This parameter uses the ICU transform string that contains the string constants that will be mentioned below.
- Reverse: If necessary, returns a reversed transformation.
In the above parameters, the focus is also difficult is the transform parameter, this parameter is a cfstringref type of parameter; to the header file, we will find that the value of this parameter has a lot
Let's briefly introduce the effects of the above values, such as:
From what we can see, using these values, we enter the left (input) language, and the result is the right (output);
Some of the above has not been introduced, it is no longer introduced, here I would like to focus on one of our above kCFStringTransformStripCombiningMarks , his function is to remove the original string of phonetic transcription or similar to the phonetic symbols.
This article main reference: http://nshipster.cn/cfstringtransform/
This article simple book Link: http://www.jianshu.com/p/a28be7b7f1d3
IOS get Pinyin cfstringtransform of Chinese characters