带声调
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
网友评论