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[] forKey:lineComponents[]];
}];
}
self->_unicodeToHanyuPinyinTable=tempMap;
[self cacheObjec:self->_unicodeToHanyuPinyinTable forKey:kCacheKeyForUnicode2Pinyin];

当我们输入汉字后,得到该汉字的编码。下面图为例。当我们点击OK按钮后

NSString *mainPinyinStrOfChar = [PinyinHelper getFirstHanyuPinyinStringWithChar:[str characterAtIndex:i] withHanyuPinyinOutputFormat:outputFormat];通过循环来生成汉字编码

从字典中查询key为“8F6C”的值为

取数组第一个就为拼音...

  使用

  1.下载库

  2.引入头文件 #import "PinYin4Objc.h"

  3.创建拼音字符格式

  HanyuPinyinOutputFormat *outputFormat=[[HanyuPinyinOutputFormat alloc] init];

  格式包含ToneType,CharType,CaseType

typedef enum {
ToneTypeWithToneNumber,//有音调数字,如PIN1
ToneTypeWithoutTone,//无音调
ToneTypeWithToneMark
}ToneType;
typedef enum {
CaseTypeUppercase,//拼音大小
CaseTypeLowercase// 拼音小写
}CaseType;
typedef enum {
VCharTypeWithUAndColon,
VCharTypeWithV,
VCharTypeWithUUnicode
}VCharType;

  4.设置格式

[outputFormat setToneType:ToneTypeWithoutTone];

[outputFormat setVCharType:VCharTypeWithV];

[outputFormat setCaseType:CaseTypeLowercase];

  5.调用toHanyuPinyinStringWithNSString:withHanyuPinyinOutputFormat:withNSString:outputBlock进行转换

[PinyinHelper toHanyuPinyinStringWithNSString:sourceText withHanyuPinyinOutputFormat:outputFormat withNSString:@" " outputBlock:^(NSString *pinYin)
{
_outputTv.text=pinYin; }];

最新文章

  1. 动态计算UITableViewCell高度
  2. Linux chkconfig命令
  3. git_sop 脚本使用说明
  4. 将 IDENTITY 转换为数据类型 int 时出现算术溢出错误。
  5. codevs-1447取出整数的一部分
  6. nginx设置不使用缓存 add_header Cache-Control no-cache
  7. Python操作excel(xlrd和xlwt)
  8. UESTC_魔法少女小蟹 CDOJ 710
  9. easyui datagrid 列的内容超出所定义的列宽时,自动换行
  10. Windows Service的官方描述,抄下来(不写obj就是LocalSystem)
  11. asp.net 给按钮 增加事件
  12. Linux Namespaces机制——实现
  13. HTML中重要的知识点,表单
  14. WebService之soap类型的服务和rest类型的服务
  15. 关于熊猫认证软件IOS安装步骤教程(适用于其他软件)
  16. windows下使用 fdfs_client 上传文件
  17. 如何设置 sass 全局变量,js如何使用 sass 变量
  18. Selenium的自我总结2_元素基本操作
  19. Swift与JS的交互
  20. sharding-jdbc读写分离原理解读

热门文章

  1. js 去掉字符串前面的0
  2. 2014中国软件开发者调查(二):Java、.NET、Web、云计算特点
  3. PHP多次调用Mysql存储过程报错解决办法
  4. Scala 深入浅出实战经典 第39讲:ListBuffer、ArrayBuffer、Queue、Stack操作代码实战
  5. 每日英语:A Whiff Of 'Welcome Home'
  6. jarsigner 签名android apk
  7. C++11实现一个自动注册的工厂
  8. 介绍几个工作开发中封装的好用的android自定义控件
  9. Mac OSX Versions输入username按1下都会出现2个字符,并且不能create,解决方法
  10. Unity3D 中 Generic 动画导入设置和 Root Motion 之间的关系