IOS get Pinyin cfstringtransform of Chinese characters

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.