iOS汉字转拼音封装

来源:互联网 发布:网络封包编辑器 编辑:程序博客网 时间:2024/06/07 00:41

引用---笔记

iOS代码中包含中文字符的排序搜索比较繁琐,引用大神的样例整理下此笔记

PinYinForObjc.h中的有两个方法,第一是将汉字全部转成拼音、第二是将汉字首字母检索出来,如下:

+ (NSString*)chineseConvertToPinYin:(NSString*)chinese;//转换为拼音

+ (NSString*)chineseConvertToPinYinHead:(NSString *)chinese;//转换为拼音首字母

详细代码:

一、(1)#import"ChineseInclude.h"

#import <Foundation/Foundation.h>@interface ChineseInclude : NSObject+ (BOOL)isIncludeChineseInString:(NSString*)str;@end

       (2)ChineseInclude.m

#import "ChineseInclude.h"@implementation ChineseInclude+ (BOOL)isIncludeChineseInString:(NSString*)str {    for (int i=0; i<str.length; i++) {        unichar ch = [str characterAtIndex:i];        if (0x4e00 < ch  && ch < 0x9fff) {            return true;        }    }    return false;}@end
二、(1)ChineseToPinyinResource.h
#ifndef _ChineseToPinyinResource_H_#define _ChineseToPinyinResource_H_@class NSArray;@class NSMutableDictionary;@interface ChineseToPinyinResource : NSObject {    NSString* _directory;    NSDictionary *_unicodeToHanyuPinyinTable;}- (id)init;- (void)initializeResource;- (NSArray *)getHanyuPinyinStringArrayWithChar:(unichar)ch;- (BOOL)isValidRecordWithNSString:(NSString *)record;- (NSString *)getHanyuPinyinRecordFromCharWithChar:(unichar)ch;+ (ChineseToPinyinResource *)getInstance;@end#endif
      (2)ChineseToPinyinResource.m
#include "ChineseToPinyinResource.h"#define LEFT_BRACKET @"("#define RIGHT_BRACKET @")"#define COMMA @","#define kCacheKeyForUnicode2Pinyin @"cache.key.for.unicode.to.pinyin"static inline NSString* cachePathForKey(NSString* directory, NSString* key) {return [directory stringByAppendingPathComponent:key];}@interface ChineseToPinyinResource ()- (id<NSCoding>)cachedObjectForKey:(NSString*)key;-(void)cacheObjec:(id<NSCoding>)obj forKey:(NSString *)key;@end@implementation ChineseToPinyinResource- (id)init {    if (self = [super init]) {        _unicodeToHanyuPinyinTable = nil;        [self initializeResource];    }    return self;}- (void)initializeResource {    NSString* cachesDirectory = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];NSString* oldCachesDirectory = [[[cachesDirectory stringByAppendingPathComponent:[[NSProcessInfo processInfo] processName]] stringByAppendingPathComponent:@"PinYinCache"] copy];    if([[NSFileManager defaultManager] fileExistsAtPath:oldCachesDirectory]) {[[NSFileManager defaultManager] removeItemAtPath:oldCachesDirectory error:NULL];}_directory = [[[cachesDirectory stringByAppendingPathComponent:[[NSBundle mainBundle] bundleIdentifier]] stringByAppendingPathComponent:@"PinYinCache"] copy];        NSDictionary *dataMap=(NSDictionary *)[self cachedObjectForKey:kCacheKeyForUnicode2Pinyin];    if (dataMap) {        self->_unicodeToHanyuPinyinTable=dataMap;    }else{        NSString *resourceName =[[NSBundle mainBundle] pathForResource:@"unicode_to_hanyu_pinyin" ofType:@"txt"];        NSString *dictionaryText=[NSString stringWithContentsOfFile:resourceName encoding:NSUTF8StringEncoding error:nil];        NSArray *lines = [dictionaryText componentsSeparatedByString:@"\r\n"];        __block NSMutableDictionary *tempMap=[[NSMutableDictionary alloc] init];        @autoreleasepool {            [lines enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {                NSArray *lineComponents=[obj componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];                [tempMap setObject:lineComponents[1] forKey:lineComponents[0]];            }];        }        self->_unicodeToHanyuPinyinTable=tempMap;        [self cacheObjec:self->_unicodeToHanyuPinyinTable forKey:kCacheKeyForUnicode2Pinyin];    }}- (id<NSCoding>)cachedObjectForKey:(NSString*)key{    NSData *data = [NSData dataWithContentsOfFile:cachePathForKey(_directory, key) options:0 error:NULL];    if (data) {           return [NSKeyedUnarchiver unarchiveObjectWithData:data];    }    return nil;}-(void)cacheObjec:(id<NSCoding>)obj forKey:(NSString *)key{    NSData* data= [NSKeyedArchiver archivedDataWithRootObject:obj];    NSString* cachePath = cachePathForKey(_directory, key);dispatch_async(dispatch_get_main_queue(), ^{        [data writeToFile:cachePath atomically:YES];    });}- (NSArray *)getHanyuPinyinStringArrayWithChar:(unichar)ch {    NSString *pinyinRecord = [self getHanyuPinyinRecordFromCharWithChar:ch];    if (nil != pinyinRecord) {        NSRange rangeOfLeftBracket= [pinyinRecord rangeOfString:LEFT_BRACKET];        NSRange rangeOfRightBracket= [pinyinRecord rangeOfString:RIGHT_BRACKET];        NSString *stripedString = [pinyinRecord substringWithRange:NSMakeRange(rangeOfLeftBracket.location+rangeOfLeftBracket.length, rangeOfRightBracket.location-rangeOfLeftBracket.location-rangeOfLeftBracket.length)];        return [stripedString componentsSeparatedByString:COMMA];    }    else return nil;}- (BOOL)isValidRecordWithNSString:(NSString *)record {    NSString *noneStr = @"(none0)";    if ((nil != record) && ![record isEqual:noneStr] && [record hasPrefix:LEFT_BRACKET] && [record hasSuffix:RIGHT_BRACKET]) {        return YES;    }    else return NO;}- (NSString *)getHanyuPinyinRecordFromCharWithChar:(unichar)ch {    int codePointOfChar = ch;    NSString *codepointHexStr =[[NSString stringWithFormat:@"%x", codePointOfChar] uppercaseString];    NSString *foundRecord =[self->_unicodeToHanyuPinyinTable objectForKey:codepointHexStr];    return [self isValidRecordWithNSString:foundRecord] ? foundRecord : nil;}+ (ChineseToPinyinResource *)getInstance {    static ChineseToPinyinResource *sharedInstance=nil;    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        sharedInstance=[[self alloc] init];    });    return sharedInstance;}@end
三、HanyuPinyinOutputFormat.h

#ifndef _HanyuPinyinOutputFormat_H_#define _HanyuPinyinOutputFormat_H_#import <Foundation/Foundation.h>typedef enum {  ToneTypeWithToneNumber,  ToneTypeWithoutTone,  ToneTypeWithToneMark}ToneType;typedef enum {    CaseTypeUppercase,    CaseTypeLowercase}CaseType;typedef enum {    VCharTypeWithUAndColon,    VCharTypeWithV,    VCharTypeWithUUnicode}VCharType;@interface HanyuPinyinOutputFormat : NSObject@property(nonatomic, assign) VCharType vCharType;@property(nonatomic, assign) CaseType caseType;@property(nonatomic, assign) ToneType toneType;- (id)init;- (void)restoreDefault;@end#endif
         HanyuPinyinOutputFormat.m

#include "HanyuPinyinOutputFormat.h"@implementation HanyuPinyinOutputFormat@synthesize vCharType=_vCharType;@synthesize caseType=_caseType;@synthesize toneType=_toneType;- (id)init {  if (self = [super init]) {    [self restoreDefault];  }  return self;}- (void)restoreDefault {    _vCharType = VCharTypeWithUAndColon;    _caseType = CaseTypeLowercase;    _toneType = ToneTypeWithToneNumber;}@end

四、NSString+PinYin4Cocoa.h

#import <Foundation/Foundation.h>@interface NSString (PinYin4Cocoa)- (NSInteger)indexOfString:(NSString *)s;- (NSInteger)indexOfString:(NSString *)s fromIndex:(int)index;- (NSInteger)indexOf:(int)ch;- (NSInteger)indexOf:(int)ch fromIndex:(int)index;+ (NSString *)valueOfChar:(unichar)value;-(NSString *) stringByReplacingRegexPattern:(NSString *)regex withString:(NSString *) replacement;-(NSString *) stringByReplacingRegexPattern:(NSString *)regex withString:(NSString *) replacement caseInsensitive:(BOOL) ignoreCase;-(NSString *) stringByReplacingRegexPattern:(NSString *)regex withString:(NSString *) replacement caseInsensitive:(BOOL) ignoreCase treatAsOneLine:(BOOL) assumeMultiLine;-(NSArray *) stringsByExtractingGroupsUsingRegexPattern:(NSString *)regex;-(NSArray *) stringsByExtractingGroupsUsingRegexPattern:(NSString *)regex caseInsensitive:(BOOL) ignoreCase treatAsOneLine:(BOOL) assumeMultiLine;-(BOOL) matchesPatternRegexPattern:(NSString *)regex;-(BOOL) matchesPatternRegexPattern:(NSString *)regex caseInsensitive:(BOOL) ignoreCase treatAsOneLine:(BOOL) assumeMultiLine;@end
       NSString+PinYin4Cocoa.m
#import "NSString+PinYin4Cocoa.h"@implementation NSString (PinYin4Cocoa)- (NSInteger)indexOfString:(NSString *)s {    NSAssert3((s!=nil), @"Error, s is a nil string, %s, %s, %d", __FILE__, __FUNCTION__, __LINE__);    if ([s length] == 0) {        return 0;    }    NSRange range = [self rangeOfString:s];    return range.location == NSNotFound ? -1 : (int) range.location;}- (NSInteger)indexOfString:(NSString *)s fromIndex:(int)index {    NSAssert3((s!=nil), @"Error, s is a nil string, %s, %s, %d", __FILE__, __FUNCTION__, __LINE__);    if ([s length] == 0) {        return 0;    }    NSRange searchRange = NSMakeRange((NSUInteger) index,                                      [self length] - (NSUInteger) index);    NSRange range = [self rangeOfString:s                                options:NSLiteralSearch                                  range:searchRange];    return range.location == NSNotFound ? -1 : (int) range.location;}- (NSInteger)indexOf:(int)ch {    return [self indexOf:ch fromIndex:0];}- (NSInteger)indexOf:(int)ch fromIndex:(int)index {    unichar c = (unichar) ch;    NSString *s = [NSString stringWithCharacters:&c length:1];    return [self indexOfString:s fromIndex:(int)index];}+ (NSString *)valueOfChar:(unichar)value {    return [NSString stringWithFormat:@"%C", value];}-(NSString *) stringByReplacingRegexPattern:(NSString *)regex withString:(NSString *) replacement caseInsensitive:(BOOL)ignoreCase {    return [self stringByReplacingRegexPattern:regex withString:replacement caseInsensitive:ignoreCase treatAsOneLine:NO];}-(NSString *) stringByReplacingRegexPattern:(NSString *)regex withString:(NSString *) replacement caseInsensitive:(BOOL) ignoreCase treatAsOneLine:(BOOL) assumeMultiLine {        NSUInteger options=0;    if (ignoreCase) {        options = options | NSRegularExpressionCaseInsensitive;    }    if (assumeMultiLine) {        options = options | NSRegularExpressionDotMatchesLineSeparators;    }        NSError *error=nil;    NSRegularExpression *pattern = [NSRegularExpression regularExpressionWithPattern:regex options:options error:&error];    if (error) {        NSLog(@"Error creating Regex: %@",[error description]);        return nil;    }        NSString *retVal= [pattern stringByReplacingMatchesInString:self options:0 range:NSMakeRange(0, [self length]) withTemplate:replacement];    return retVal;}-(NSString *) stringByReplacingRegexPattern:(NSString *)regex withString:(NSString *) replacement {    return [self stringByReplacingRegexPattern:regex withString:replacement caseInsensitive:NO treatAsOneLine:NO];}-(NSArray *) stringsByExtractingGroupsUsingRegexPattern:(NSString *)regex caseInsensitive:(BOOL) ignoreCase treatAsOneLine:(BOOL) assumeMultiLine {    NSUInteger options=0;    if (ignoreCase) {        options = options | NSRegularExpressionCaseInsensitive;    }    if (assumeMultiLine) {        options = options | NSRegularExpressionDotMatchesLineSeparators;    }        NSError *error=nil;    NSRegularExpression *pattern = [NSRegularExpression regularExpressionWithPattern:regex options:options error:&error];    if (error) {        NSLog(@"Error creating Regex: %@",[error description]);        return nil;    }        __block NSMutableArray *retVal = [NSMutableArray array];    [pattern enumerateMatchesInString:self options:0 range:NSMakeRange(0, [self length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {        //Note, we only want to return the things in parens, so we're skipping index 0 intentionally        for (int i=1; i<[result numberOfRanges]; i++) {            NSString *matchedString=[self substringWithRange:[result rangeAtIndex:i]];            [retVal addObject:matchedString];        }    }];    return retVal;}-(NSArray *) stringsByExtractingGroupsUsingRegexPattern:(NSString *)regex {    return [self stringsByExtractingGroupsUsingRegexPattern:regex caseInsensitive:NO treatAsOneLine:NO];}-(BOOL) matchesPatternRegexPattern:(NSString *)regex caseInsensitive:(BOOL) ignoreCase treatAsOneLine:(BOOL) assumeMultiLine {    NSUInteger options=0;    if (ignoreCase) {        options = options | NSRegularExpressionCaseInsensitive;    }    if (assumeMultiLine) {        options = options | NSRegularExpressionDotMatchesLineSeparators;    }        NSError *error=nil;    NSRegularExpression *pattern = [NSRegularExpression regularExpressionWithPattern:regex options:options error:&error];    if (error) {        NSLog(@"Error creating Regex: %@",[error description]);        return NO;  //Can't possibly match an invalid Regex    }        return ([pattern numberOfMatchesInString:self options:0 range:NSMakeRange(0, [self length])] > 0);}-(BOOL) matchesPatternRegexPattern:(NSString *)regex {    return [self matchesPatternRegexPattern:regex caseInsensitive:NO treatAsOneLine:NO];}
五、PinYin4Objc.h
#import "HanyuPinyinOutputFormat.h"#import "PinyinHelper.h"
六、PinyinFormatter.h
#ifndef _PinyinFormatter_H_#define _PinyinFormatter_H_@class HanyuPinyinOutputFormat;@interface PinyinFormatter : NSObject {}+ (NSString *)formatHanyuPinyinWithNSString:(NSString *)pinyinStr                withHanyuPinyinOutputFormat:(HanyuPinyinOutputFormat *)outputFormat;+ (NSString *)convertToneNumber2ToneMarkWithNSString:(NSString *)pinyinStr;- (id)init;@end#endif
       PinyinFormatter.m

#include "HanyuPinyinOutputFormat.h"#include "PinyinFormatter.h"#import "NSString+PinYin4Cocoa.h"@interface PinyinFormatter ()+(NSInteger)getNumericValue:(unichar)c;+(NSInteger)indexOfChar:(int*) table ch:(unichar)c;@end    @implementation PinyinFormatterstatic int numericKeys[] = {    0x0030, 0x0041, 0x0061, 0x00b2, 0x00b9, 0x00bc, 0x0660, 0x06f0,    0x0966, 0x09e6, 0x09f4, 0x09f9, 0x0a66, 0x0ae6, 0x0b66, 0x0be7,    0x0bf1, 0x0bf2, 0x0c66, 0x0ce6, 0x0d66, 0x0e50, 0x0ed0, 0x0f20,    0x1040, 0x1369, 0x1373, 0x1374, 0x1375, 0x1376, 0x1377, 0x1378,    0x1379, 0x137a, 0x137b, 0x137c, 0x16ee, 0x17e0, 0x1810, 0x2070,    0x2074, 0x2080, 0x2153, 0x215f, 0x2160, 0x216c, 0x216d, 0x216e,    0x216f, 0x2170, 0x217c, 0x217d, 0x217e, 0x217f, 0x2180, 0x2181,    0x2182, 0x2460, 0x2474, 0x2488, 0x24ea, 0x2776, 0x2780, 0x278a,    0x3007, 0x3021, 0x3038, 0x3039, 0x303a, 0x3280, 0xff10, 0xff21,    0xff41,};static unichar numericValues[] = {    0x0039, 0x0030, 0x005a, 0x0037, 0x007a, 0x0057, 0x00b3, 0x00b0,    0x00b9, 0x00b8, 0x00be, 0x0000, 0x0669, 0x0660, 0x06f9, 0x06f0,    0x096f, 0x0966, 0x09ef, 0x09e6, 0x09f7, 0x09f3, 0x09f9, 0x09e9,    0x0a6f, 0x0a66, 0x0aef, 0x0ae6, 0x0b6f, 0x0b66, 0x0bf0, 0x0be6,    0x0bf1, 0x0b8d, 0x0bf2, 0x080a, 0x0c6f, 0x0c66, 0x0cef, 0x0ce6,    0x0d6f, 0x0d66, 0x0e59, 0x0e50, 0x0ed9, 0x0ed0, 0x0f29, 0x0f20,    0x1049, 0x1040, 0x1372, 0x1368, 0x1373, 0x135f, 0x1374, 0x1356,    0x1375, 0x134d, 0x1376, 0x1344, 0x1377, 0x133b, 0x1378, 0x1332,    0x1379, 0x1329, 0x137a, 0x1320, 0x137b, 0x1317, 0x137c, 0xec6c,    0x16f0, 0x16dd, 0x17e9, 0x17e0, 0x1819, 0x1810, 0x2070, 0x2070,    0x2079, 0x2070, 0x2089, 0x2080, 0x215e, 0x0000, 0x215f, 0x215e,    0x216b, 0x215f, 0x216c, 0x213a, 0x216d, 0x2109, 0x216e, 0x1f7a,    0x216f, 0x1d87, 0x217b, 0x216f, 0x217c, 0x214a, 0x217d, 0x2119,    0x217e, 0x1f8a, 0x217f, 0x1d97, 0x2180, 0x1d98, 0x2181, 0x0df9,    0x2182, 0xfa72, 0x2473, 0x245f, 0x2487, 0x2473, 0x249b, 0x2487,    0x24ea, 0x24ea, 0x277f, 0x2775, 0x2789, 0x277f, 0x2793, 0x2789,    0x3007, 0x3007, 0x3029, 0x3020, 0x3038, 0x302e, 0x3039, 0x3025,    0x303a, 0x301c, 0x3289, 0x327f, 0xff19, 0xff10, 0xff3a, 0xff17,    0xff5a, 0xff37,};+ (NSString *)formatHanyuPinyinWithNSString:(NSString *)pinyinStr                withHanyuPinyinOutputFormat:(HanyuPinyinOutputFormat *)outputFormat {  if ((ToneTypeWithToneMark == [outputFormat toneType]) && ((VCharTypeWithV == [outputFormat vCharType]) || (VCharTypeWithUAndColon == [outputFormat vCharType]))) {      @throw [NSException exceptionWithName:@"Throwing a BadHanyuPinyinOutputFormatCombination exception" reason:@"tone marks cannot be added to v or u:." userInfo:nil];  }  if (ToneTypeWithoutTone == [outputFormat toneType]) {      pinyinStr =[pinyinStr stringByReplacingOccurrencesOfString:@"[1-5]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, pinyinStr.length)];  }  else if (ToneTypeWithToneMark == [outputFormat toneType]) {      pinyinStr =[pinyinStr stringByReplacingOccurrencesOfString:@"u:" withString:@"v"];     pinyinStr = [PinyinFormatter convertToneNumber2ToneMarkWithNSString:pinyinStr];  }  if (VCharTypeWithV == [outputFormat vCharType]) {      pinyinStr =[pinyinStr stringByReplacingOccurrencesOfString:@"u:" withString:@"v"];  }  else if (VCharTypeWithUUnicode == [outputFormat vCharType]) {      pinyinStr =[pinyinStr stringByReplacingOccurrencesOfString:@"u:" withString:@"ü"];  }  if (CaseTypeUppercase == [outputFormat caseType]) {    pinyinStr = [pinyinStr uppercaseString];  }  return pinyinStr;}+ (NSString *)convertToneNumber2ToneMarkWithNSString:(NSString *)pinyinStr {  NSString *lowerCasePinyinStr = [pinyinStr lowercaseString];  if ([lowerCasePinyinStr matchesPatternRegexPattern:@"[a-z]*[1-5]?"]) {    unichar defautlCharValue = '$';    int defautlIndexValue = -1;    unichar unmarkedVowel = defautlCharValue;    int indexOfUnmarkedVowel = defautlIndexValue;    unichar charA = 'a';    unichar charE = 'e';    NSString *ouStr = @"ou";    NSString *allUnmarkedVowelStr = @"aeiouv";    NSString *allMarkedVowelStr = @"āáăàaēéĕèeīíĭìiōóŏòoūúŭùuǖǘǚǜü";    if ([lowerCasePinyinStr matchesPatternRegexPattern:@"[a-z]*[1-5]"]) {        int tuneNumber = [PinyinFormatter getNumericValue:[lowerCasePinyinStr characterAtIndex:lowerCasePinyinStr.length -1]];      int indexOfA = [lowerCasePinyinStr indexOf:charA];      int indexOfE = [lowerCasePinyinStr indexOf:charE];      int ouIndex = [lowerCasePinyinStr indexOfString:ouStr];      if (-1 != indexOfA) {        indexOfUnmarkedVowel = indexOfA;        unmarkedVowel = charA;      }      else if (-1 != indexOfE) {        indexOfUnmarkedVowel = indexOfE;        unmarkedVowel = charE;      }      else if (-1 != ouIndex) {        indexOfUnmarkedVowel = ouIndex;        unmarkedVowel = [ouStr characterAtIndex:0];      }      else {        for (int i = [lowerCasePinyinStr length] - 1; i >= 0; i--) {          if ([[NSString valueOfChar:[lowerCasePinyinStr characterAtIndex:i]] matchesPatternRegexPattern:@"[aeiouv]"]) {            indexOfUnmarkedVowel = i;            unmarkedVowel = [lowerCasePinyinStr characterAtIndex:i];            break;          }        }      }      if ((defautlCharValue != unmarkedVowel) && (defautlIndexValue != indexOfUnmarkedVowel)) {        int rowIndex = [allUnmarkedVowelStr indexOf:unmarkedVowel];        int columnIndex = tuneNumber - 1;        int vowelLocation = rowIndex * 5 + columnIndex;        unichar markedVowel = [allMarkedVowelStr characterAtIndex:vowelLocation];        NSMutableString *resultBuffer = [[NSMutableString alloc] init];          [resultBuffer appendString:[[lowerCasePinyinStr substringToIndex:indexOfUnmarkedVowel+1] stringByReplacingOccurrencesOfString:@"v" withString:@"ü"]];        [resultBuffer appendFormat:@"%C",markedVowel];          [resultBuffer appendString:[[lowerCasePinyinStr substringWithRange:NSMakeRange(indexOfUnmarkedVowel + 1, lowerCasePinyinStr.length-indexOfUnmarkedVowel)] stringByReplacingOccurrencesOfString:@"v" withString:@"ü"]];        return [resultBuffer description];      }      else {        return lowerCasePinyinStr;      }    }    else {      return [lowerCasePinyinStr stringByReplacingOccurrencesOfString:@"v" withString:@"ü"];    }  }  else {    return lowerCasePinyinStr;  }}+(NSInteger)getNumericValue:(unichar)c{    if (c < 128) {        // Optimized for ASCII        if (c >= '0' && c <= '9') {            return c - '0';        }        if (c >= 'a' && c <= 'z') {            return c - ('a' - 10);        }        if (c >= 'A' && c <= 'Z') {            return c - ('A' - 10);        }        return -1;    }    NSInteger result = [self indexOfChar:numericKeys ch:c];    if (result >= 0 && c <= numericValues[result * 2]) {        unichar difference = numericValues[result * 2 + 1];        if (difference == 0) {            return -2;        }        // Value is always positive, must be negative value        if (difference > c) {            return c - (short) difference;        }        return c - difference;    }    return -1;}+(NSInteger)indexOfChar:(int*) table ch:(unichar)c{    NSInteger len=sizeof(table)/sizeof(table[0]);    for (int i = 0; i < len; i++) {        if (table[i] == (int) c) {            return i;        }    }    return -1;}- (id)init {  return [super init];}@end
七、PinYinForObjc.h
#import <Foundation/Foundation.h>#import "HanyuPinyinOutputFormat.h"#import "PinyinHelper.h"@interface PinYinForObjc : NSObject+ (NSString*)chineseConvertToPinYin:(NSString*)chinese;//转换为拼音+ (NSString*)chineseConvertToPinYinHead:(NSString *)chinese;//转换为拼音首字母@end
      PinYinForObjc.m
#import "PinYinForObjc.h"@implementation PinYinForObjc+ (NSString*)chineseConvertToPinYin:(NSString*)chinese {    NSString *sourceText = chinese;    HanyuPinyinOutputFormat *outputFormat = [[HanyuPinyinOutputFormat alloc] init];    [outputFormat setToneType:ToneTypeWithoutTone];    [outputFormat setVCharType:VCharTypeWithV];    [outputFormat setCaseType:CaseTypeLowercase];    NSString *outputPinyin = [PinyinHelper toHanyuPinyinStringWithNSString:sourceText withHanyuPinyinOutputFormat:outputFormat withNSString:@""];        return outputPinyin;        }+ (NSString*)chineseConvertToPinYinHead:(NSString *)chinese {    HanyuPinyinOutputFormat *outputFormat = [[HanyuPinyinOutputFormat alloc] init];    [outputFormat setToneType:ToneTypeWithoutTone];    [outputFormat setVCharType:VCharTypeWithV];    [outputFormat setCaseType:CaseTypeLowercase];    NSMutableString *outputPinyin = [[NSMutableString alloc] init];    for (int i=0;i <chinese.length;i++) {        NSString *mainPinyinStrOfChar = [PinyinHelper getFirstHanyuPinyinStringWithChar:[chinese characterAtIndex:i] withHanyuPinyinOutputFormat:outputFormat];        if (nil!=mainPinyinStrOfChar) {            [outputPinyin appendString:[mainPinyinStrOfChar substringToIndex:1]];        }    }    // 如果是不是拼音需要判断到底是哪个英文开头的 如果数字特殊服的默认返回#    if (outputPinyin.length==0) {        outputPinyin = (NSMutableString*)[self englishToHeade:chinese];    }        return  [[outputPinyin substringToIndex:1] uppercaseString];}+ (NSString*)englishToHeade:(NSString*)string{    NSString *firstStr = [[string substringToIndex:1] uppercaseString];    return [self isKindOfChar:firstStr]?firstStr:@"#";}+ (BOOL)isKindOfChar:(NSString*)str{    NSArray *array = @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"G",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"];    if ([array containsObject:str]) {        return YES;    }else{        return NO;    }}@end
八、PinyinHelper.h
#ifndef _PinyinHelper_H_#define _PinyinHelper_H_@class HanyuPinyinOutputFormat;@interface PinyinHelper : NSObject {}+ (NSArray *)toHanyuPinyinStringArrayWithChar:(unichar)ch;+ (NSArray *)toHanyuPinyinStringArrayWithChar:(unichar)ch                         withHanyuPinyinOutputFormat:(HanyuPinyinOutputFormat *)outputFormat;+ (NSArray *)getFormattedHanyuPinyinStringArrayWithChar:(unichar)ch                                   withHanyuPinyinOutputFormat:(HanyuPinyinOutputFormat *)outputFormat;+ (NSArray *)getUnformattedHanyuPinyinStringArrayWithChar:(unichar)ch;+ (NSArray *)toTongyongPinyinStringArrayWithChar:(unichar)ch;+ (NSArray *)toWadeGilesPinyinStringArrayWithChar:(unichar)ch;+ (NSArray *)toMPS2PinyinStringArrayWithChar:(unichar)ch;+ (NSArray *)toYalePinyinStringArrayWithChar:(unichar)ch;+ (NSArray *)convertToTargetPinyinStringArrayWithChar:(unichar)ch                                  withPinyinRomanizationType:(NSString *)targetPinyinSystem;+ (NSArray *)toGwoyeuRomatzyhStringArrayWithChar:(unichar)ch;+ (NSArray *)convertToGwoyeuRomatzyhStringArrayWithChar:(unichar)ch;+ (NSString *)toHanyuPinyinStringWithNSString:(NSString *)str                  withHanyuPinyinOutputFormat:(HanyuPinyinOutputFormat *)outputFormat                                 withNSString:(NSString *)seperater;+ (NSString *)getFirstHanyuPinyinStringWithChar:(unichar)ch                    withHanyuPinyinOutputFormat:(HanyuPinyinOutputFormat *)outputFormat;- (id)init;@end#endif
       PinyinHelper.m
#include "ChineseToPinyinResource.h"#include "HanyuPinyinOutputFormat.h"#include "PinyinFormatter.h"#include "PinyinHelper.h"#define HANYU_PINYIN @"Hanyu"#define WADEGILES_PINYIN @"Wade"#define MPS2_PINYIN @"MPSII"#define YALE_PINYIN @"Yale"#define TONGYONG_PINYIN @"Tongyong"#define GWOYEU_ROMATZYH @"Gwoyeu"@implementation PinyinHelper+ (NSArray *)toHanyuPinyinStringArrayWithChar:(unichar)ch {    return [PinyinHelper getUnformattedHanyuPinyinStringArrayWithChar:ch];}+ (NSArray *)toHanyuPinyinStringArrayWithChar:(unichar)ch                  withHanyuPinyinOutputFormat:(HanyuPinyinOutputFormat *)outputFormat {    return [PinyinHelper getFormattedHanyuPinyinStringArrayWithChar:ch withHanyuPinyinOutputFormat:outputFormat];}+ (NSArray *)getFormattedHanyuPinyinStringArrayWithChar:(unichar)ch                            withHanyuPinyinOutputFormat:(HanyuPinyinOutputFormat *)outputFormat {    NSMutableArray *pinyinStrArray =[NSMutableArray arrayWithArray:[PinyinHelper getUnformattedHanyuPinyinStringArrayWithChar:ch]];    if (nil != pinyinStrArray) {        for (int i = 0; i < (int) [pinyinStrArray count]; i++) {            [pinyinStrArray replaceObjectAtIndex:i withObject:[PinyinFormatter formatHanyuPinyinWithNSString:                                                               [pinyinStrArray objectAtIndex:i]withHanyuPinyinOutputFormat:outputFormat]];        }        return pinyinStrArray;    }    else return nil;}+ (NSArray *)getUnformattedHanyuPinyinStringArrayWithChar:(unichar)ch {    return [[ChineseToPinyinResource getInstance] getHanyuPinyinStringArrayWithChar:ch];}+ (NSArray *)toTongyongPinyinStringArrayWithChar:(unichar)ch {    return [PinyinHelper convertToTargetPinyinStringArrayWithChar:ch withPinyinRomanizationType: TONGYONG_PINYIN];}+ (NSArray *)toWadeGilesPinyinStringArrayWithChar:(unichar)ch {    return [PinyinHelper convertToTargetPinyinStringArrayWithChar:ch withPinyinRomanizationType: WADEGILES_PINYIN];}+ (NSArray *)toMPS2PinyinStringArrayWithChar:(unichar)ch {    return [PinyinHelper convertToTargetPinyinStringArrayWithChar:ch withPinyinRomanizationType: MPS2_PINYIN];}+ (NSArray *)toYalePinyinStringArrayWithChar:(unichar)ch {    return [PinyinHelper convertToTargetPinyinStringArrayWithChar:ch withPinyinRomanizationType: YALE_PINYIN];}+ (NSArray *)convertToTargetPinyinStringArrayWithChar:(unichar)ch                           withPinyinRomanizationType:(NSString *)targetPinyinSystem {    NSArray *hanyuPinyinStringArray = [PinyinHelper getUnformattedHanyuPinyinStringArrayWithChar:ch];    if (nil != hanyuPinyinStringArray) {        NSMutableArray *targetPinyinStringArray = [NSMutableArray arrayWithCapacity:hanyuPinyinStringArray.count];        for (int i = 0; i < (int) [hanyuPinyinStringArray count]; i++) {                    }        return targetPinyinStringArray;    }    else return nil;}+ (NSArray *)toGwoyeuRomatzyhStringArrayWithChar:(unichar)ch {    return [PinyinHelper convertToGwoyeuRomatzyhStringArrayWithChar:ch];}+ (NSArray *)convertToGwoyeuRomatzyhStringArrayWithChar:(unichar)ch {    NSArray *hanyuPinyinStringArray = [PinyinHelper getUnformattedHanyuPinyinStringArrayWithChar:ch];    if (nil != hanyuPinyinStringArray) {        NSMutableArray *targetPinyinStringArray =[NSMutableArray arrayWithCapacity:hanyuPinyinStringArray.count];        for (int i = 0; i < (int) [hanyuPinyinStringArray count]; i++) {        }        return targetPinyinStringArray;    }    else return nil;}+ (NSString *)toHanyuPinyinStringWithNSString:(NSString *)str                  withHanyuPinyinOutputFormat:(HanyuPinyinOutputFormat *)outputFormat                                 withNSString:(NSString *)seperater {    NSMutableString *resultPinyinStrBuf = [[NSMutableString alloc] init];    for (int i = 0; i <  str.length; i++) {        NSString *mainPinyinStrOfChar = [PinyinHelper getFirstHanyuPinyinStringWithChar:[str characterAtIndex:i] withHanyuPinyinOutputFormat:outputFormat];        if (nil != mainPinyinStrOfChar) {            [resultPinyinStrBuf appendString:mainPinyinStrOfChar];            if (i != [str length] - 1) {                [resultPinyinStrBuf appendString:seperater];            }        }        else {            [resultPinyinStrBuf appendFormat:@"%C",[str characterAtIndex:i]];        }    }    return resultPinyinStrBuf;}+ (NSString *)getFirstHanyuPinyinStringWithChar:(unichar)ch                    withHanyuPinyinOutputFormat:(HanyuPinyinOutputFormat *)outputFormat {    NSArray *pinyinStrArray = [PinyinHelper getFormattedHanyuPinyinStringArrayWithChar:ch withHanyuPinyinOutputFormat:outputFormat];    if ((nil != pinyinStrArray) && ((int) [pinyinStrArray count] > 0)) {        return [pinyinStrArray objectAtIndex:0];    }    else {        return nil;    }}- (id)init {    return [super init];}@end
九、unicode_to_hanyu_pinyin.txt文件下载-----》http://pan.baidu.com/s/1mhVQ52g 提取密码5tui

原创粉丝点击