iOS CFStringTransform汉字转拼音

带声调

    NSString *chineseCharacterString = @"我爱我的祖国";
    if (chineseCharacterString.length > 0) {
        NSMutableString *sb = [[NSMutableString alloc] initWithString:chineseCharacterString];
        if (CFStringTransform((__bridge CFMutableStringRef)sb, 0, kCFStringTransformMandarinLatin, NO)) {
            NSLog(@"spelling: %@", sb);
        }
    }

输出结果:spelling: wǒ ài wǒ de zǔ guó

不带声调

    NSString *chineseCharacterString = @"我爱我的祖国";
    if (chineseCharacterString.length > 0) {
        NSMutableString *sb = [[NSMutableString alloc] initWithString:chineseCharacterString];
        CFStringTransform((__bridge CFMutableStringRef)sb, 0, kCFStringTransformMandarinLatin, NO);
        if (CFStringTransform((__bridge CFMutableStringRef)sb, 0, kCFStringTransformStripDiacritics, NO)) {
            NSLog(@"spelling: %@", sb);
        }
    }

输出结果:spelling: wo ai wo de zu guo

备注:

CFStringTransform还可进行其他语言的转换,请参考NSHipster

你可能感兴趣的:(iOS CFStringTransform汉字转拼音)