In the development of iOS often involves the ordering of Chinese characters, the most common is to be based on the first letter of the order of characters, such as common contacts and so on. Summed up, can be broadly divided into two methods, of which the reference [1] provides a very complex method, and the code is very large, not recommended. Another approach is to use the method provided in the core Foundation directly, which is very simple, with the following code:
1-(NSString *) Transform: (NSString *) chinese{2 //Replace the nsstring with nsmutablestring3Nsmutablestring *pinyin =[Chinese mutablecopy];4 5 //convert Chinese characters to pinyin (bands label)6 Cfstringtransform ((__bridge cfmutablestringref) Pinyin, NULL, Kcfstringtransformmandarinlatin, NO);7NSLog (@"%@", pinyin);8 9 //Remove phonetic transcription of pinyinTen Cfstringtransform ((__bridge cfmutablestringref) Pinyin, NULL, Kcfstringtransformstripcombiningmarks, NO); OneNSLog (@"%@", pinyin); A - //Return Recent Results - returnPinyin; the}
This function can translate Chinese characters into pinyin and support mixed input of English and Chinese characters, such as passing the string "Hello, China!" ", the output of the function is:
£ º37.903 uitest[1867:20332 ] Hello, Zhōng guó! £ º£45.845 uitest[1867: 20332] Hello, Zhong guo!
Resources:
[1] rainbownight, iOS get Chinese pinyin initials, 51CTO
[2] Tanyufeng, ios get pinyin for Chinese characters, Pinterest
[3] Matt Thompson, Cfstringtransform, nshipster.cn
iOS get pinyin for Chinese characters