PinYinForObjc.m 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //
  2. // PinYinForObjc.m
  3. // Search
  4. //
  5. // Created by LYZ on 14-1-24.
  6. // Copyright (c) 2014年 LYZ. All rights reserved.
  7. //
  8. #import "PinYinForObjc.h"
  9. @implementation PinYinForObjc
  10. + (NSString*)chineseConvertToPinYin:(NSString*)chinese {
  11. NSString *sourceText = chinese;
  12. HanyuPinyinOutputFormat *outputFormat = [[HanyuPinyinOutputFormat alloc] init];
  13. [outputFormat setToneType:ToneTypeWithoutTone];
  14. [outputFormat setVCharType:VCharTypeWithV];
  15. [outputFormat setCaseType:CaseTypeLowercase];
  16. NSString *outputPinyin = [PinyinHelper toHanyuPinyinStringWithNSString:sourceText withHanyuPinyinOutputFormat:outputFormat withNSString:@""];
  17. return outputPinyin;
  18. }
  19. + (NSString*)chineseConvertToPinYinHead:(NSString *)chinese {
  20. HanyuPinyinOutputFormat *outputFormat = [[HanyuPinyinOutputFormat alloc] init];
  21. [outputFormat setToneType:ToneTypeWithoutTone];
  22. [outputFormat setVCharType:VCharTypeWithV];
  23. [outputFormat setCaseType:CaseTypeLowercase];
  24. NSMutableString *outputPinyin = [[NSMutableString alloc] init];
  25. for (int i=0;i <chinese.length;i++) {
  26. NSString *mainPinyinStrOfChar = [PinyinHelper getFirstHanyuPinyinStringWithChar:[chinese characterAtIndex:i] withHanyuPinyinOutputFormat:outputFormat];
  27. if (nil!=mainPinyinStrOfChar) {
  28. [outputPinyin appendString:[mainPinyinStrOfChar substringToIndex:1]];
  29. } else {
  30. break;
  31. }
  32. }
  33. return outputPinyin;
  34. }
  35. @end