No more nonsense to say, directly to everyone to paste the key code.
The specific code looks like this:
#import <Foundation/Foundation.h>
@interface nsstring (Utils)
/**
* Pinyin of Chinese characters * *
@return Pinyin * *
-(NSString *) Pinyin;
@end
#import "nsstring+utils.h"
@implementation nsstring (Utils)
//Pinyin of Chinese characters
-(NSString *) pinyin{
nsmutablestring *str = [self mutablecopy];
Cfstringtransform ((cfmutablestringref) str, NULL, Kcfstringtransformmandarinlatin, NO);
Cfstringtransform ((cfmutablestringref) str, NULL, Kcfstringtransformstripdiacritics, NO);
return [str stringbyreplacingoccurrencesofstring:@ "" withstring:@ "];
}
@end
Next look at iOS to convert Chinese characters into pinyin
In the development of iOS often encounter do address book needs to convert Chinese characters into Pinyin, the following is the way I converted Chinese characters into pinyin
+ (NSString *) Transform: (NSString *) Chinese
{nsmutablestring *pinyin
= [Chinese mutablecopy];
Cfstringtransform ((__bridge cfmutablestringref) Pinyin, NULL, Kcfstringtransformmandarinlatin, NO);
Cfstringtransform ((__bridge cfmutablestringref) Pinyin, NULL, Kcfstringtransformstripcombiningmarks, NO);
NSLog (@ "%@", pinyin);
return [Pinyin uppercasestring];
}
The Kcfstringtransformmandarinlatin method is to convert the pinyin of Shiying, if you need to remove the phonetic transcription, then continue to use the Kcfstringtransformstripcombiningmarks method.
The above is a small set to introduce the Chinese pinyin of iOS, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!