PinYin4Objc是一个在git汉字转拼音的开源库,支持简体和繁体中文。效率POAPinyin等其他库要高,转换库也完整下面简单介绍 实现原理使用unicode_to_hanyu_pinyin.txt存储汉字编码相对应的拼音,以字典加载到内存中 NSString *r ...
PinYin4Objc是一个在git汉字转拼音的开源库,支持简体和繁体中文。效率POAPinyin等其他库要高,转换库也完整下面简单介绍
实现原理
使用unicode_to_hanyu_pinyin.txt存储汉字编码相对应的拼音,以字典加载到内存中
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];
原标题:【推荐】iOS汉字转拼音第三方库
关键词:IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。