ios汉字转拼音,支持多音节

1 + (NSString *)transform:(NSString *)chinese
2 {
3     NSMutableString *pinyin = [chinese mutableCopy];
4     CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO);
5     CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO);
6     NSLog(@"%@", pinyin);
7     return [pinyin uppercaseString];
8 }

kCFStringTransformMandarinLatin方法转化出来的是带音标的拼音,如果需要去掉音标,则继续使用kCFStringTransformStripCombiningMarks方法即可。
但是这种方式性能可能会比较差。推荐使用第三方的使用 -------------->>试试这个第三方:https://github.com/kimziv/PinYin4Objc,支持多音字


下面自己做小Demo,可以 参考下面代码
NSString *sourceText=@"我爱中文";
  HanyuPinyinOutputFormat *outputFormat=[[HanyuPinyinOutputFormat alloc] init];
  [outputFormat setToneType:ToneTypeWithoutTone];
  [outputFormat setVCharType:VCharTypeWithV];
  [outputFormat setCaseType:CaseTypeLowercase];    
  [PinyinHelper toHanyuPinyinStringWithNSString:sourceText 
                     withHanyuPinyinOutputFormat:outputFormat 
                                     withNSString:@" " 
                                     outputBlock:^(NSString *pinYin) {
      _outputTv.text=pinYin; //update ui

   }];

 

 
posted @ 2016-11-03 17:49  黄啊  阅读(3438)  评论(0编辑  收藏  举报