代码如下
- (void)viewDidLoad {
[super viewDidLoad];
NSString *chineseText = @"我爱编程";
if ([chineseText length]) {
NSMutableString *pinyinText = [[NSMutableString alloc] initWithString:chineseText];
// 转换后拼音带声调
if (CFStringTransform((__bridge CFMutableStringRef)pinyinText, 0, kCFStringTransformMandarinLatin, NO)) {
NSLog(@"%@", pinyinText);
}
// 转换后拼音不带声调
if (CFStringTransform((__bridge CFMutableStringRef)pinyinText, 0, kCFStringTransformStripDiacritics, NO)) {
NSLog(@"%@", pinyinText);
}
}
}
运行结果如下
wǒ ài biān chéng
wo ai bian cheng