In the app development, sometimes we need to convert Chinese characters into corresponding pinyin, for example, in the Address book, according to the contact name to sort contacts contact information, how to sort contacts, in general we need to translate the contacts into Hanyu Pinyin, and then according to the order of pinyin, the contact sort. (The following picture tells the principle of transformation, the specific code has been helped us to achieve, http://files.cnblogs.com/files/wangyaoguo/ChineseSort.zip)
The above method can be used to sort Chinese characters according to Pinyin, but the above method is implemented using a third-party encapsulated file, in fact, the system has helped us to implement the above method, we can use
Cfstringtransform this class. (Code snippet One is a tone of Hanyu Pinyin, code snippet two is without tone of pinyin)
1 nsmutablestring *mutablestring = [nsmutablestring stringwithstring:@ "Code changes the World"];2 Cfstringtransform ( CFMUTABLESTRINGREF) mutablestring, NULL, Kcfstringtransformtolatin, False), 3 NSLog (@ "%@", mutablestring);
1 nsmutablestring *mutablestring = [nsmutablestring stringwithstring:@ "Code changes the World"];2 Cfstringtransform ( CFMUTABLESTRINGREF) mutablestring, NULL, Kcfstringtransformtolatin, false); 3 Cfstringtransform (( CFMUTABLESTRINGREF) mutablestring, NULL, Kcfstringtransformstripdiacritics, false); 4 NSLog (@ "%@", mutablestring) ;
ios-Kanji to Pinyin