文字转拼音
2018-08-30 本文已影响15人
英雄出少年
@interface TGConvertPY : NSObject
+ (TGConvertPY *)sharedInstance;
- (NSString *)toPinyin: (NSString *)source;
@end
@interface TGConvertPY()
{
int *_codeIndex;
char *_pinyin;
BOOL _inited;
}
@end
@implementation TGConvertPY
+ (TGConvertPY *)sharedInstance
{
static TGConvertPY *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[TGConvertPY alloc] init];
});
return instance;
}
- (NSString *)toPinyin: (NSString *)source
{
if ([source length] == 0)
{
return nil;
}
NSMutableString *mutableString = [NSMutableString stringWithString:source];
CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformToLatin, false);
mutableString = (NSMutableString *)[mutableString stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:[NSLocale currentLocale]];
return [mutableString stringByReplacingOccurrencesOfString:@"'" withString:@""];
}