码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2016-11-03 19:04:49      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:any   mst   format   ati   nbsp   较差   汉字   table   tab   

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

   }];

 

 

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

标签:any   mst   format   ati   nbsp   较差   汉字   table   tab   

原文地址:http://www.cnblogs.com/huangzhenwei/p/6027577.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!