美文网首页
文字转拼音

文字转拼音

作者: 英雄出少年 | 来源:发表于2018-08-30 10:58 被阅读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:@""];
    }
    

    相关文章

      网友评论

          本文标题:文字转拼音

          本文链接:https://www.haomeiwen.com/subject/ofjrwftx.html