【推荐】iOS汉字转拼音第三方库
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; }];
【推荐】iOS汉字转拼音第三方库的更多相关文章
- IOS学习:常用第三方库(GDataXMLNode:xml解析库)
IOS学习:常用第三方库(GDataXMLNode:xml解析库) 解析 XML 通常有两种方式,DOM 和 SAX: DOM解析XML时,读入整个XML文档并构建一个驻留内存的树结构(节点树),通过 ...
- iOS非常全的第三方库
iOS ● 非常全的三方库.插件.大牛博客等等 github排名:https://github.com/trending, github搜索:https://github.com/search. ...
- iOS 中 常用的第三方库
现在对于我们 iOS 开发来说,基本上说不可能不使用第三方轮子啦,毕竟没那么多时间,而且自己造的轮子往往想着成为上图中的最后一个,结果却成了上图中第二个或第一个啦,当然大公司另当别论.下面我从之前用过 ...
- iOS 汉字的拼音
获取汉字的拼音 #import <Foundation/Foundation.h> @interface NSString (Utils) /** * 汉字的拼音 * * @return ...
- iOS——Xcode中添加第三方库
一.只有.h和.a文件的库 1.向项目中添加三方库文件 如果添加的第三方库只有.h和.a文件,直接把文件夹拖进项目下面,这时会弹出下面的提示框,一定要勾选下面选择的选项: 这里要注意,在Add to ...
- iOS import导入pod第三方库不提示问题
pod 导入第三方库后,使用import 不提示第三方库头文件. 解决办法: 选择target -> BuildSettings -> search Paths 下的 User Heade ...
- iOS 汉字转拼音 PinYin4Objc
PinYin4Objc 是一个效率很高的汉字转拼音类库,支持简体和繁体中文.有以下特性:1.效率高,使用数据缓存,第一次初始化以后,拼音数据存入文件缓存和内存缓存,后面转换效率大大提高:2.支持自定义 ...
- iOS 推荐一个下载用的第三方库
AFNetworking有下载功能,但是下载功能比较基本,要实现复杂下载功能需要自己写一些代码.今天在github上找到了一个下载功能的开源项目,非常不错,链接如下:https://github.co ...
- ios常用到的第三方库
在iOS开发中不可避免的会用到一些第三方类库,它们提供了很多实用的功能,使我们的开发变得更有效率:同时,也可以从它们的源代码中学习到很多有用的东西. Reachability 检测网络连接 用来检查网 ...
随机推荐
- Linux录屏软件
如何查找录屏软件 apt-cache search screen record libutempter-dev - privileged helper for utmp/wtmp updates (d ...
- Android--使用Canvas绘图
前言 除了使用已有的图片之外,Android应用常常需要在运行时根据场景动态生成2D图片,比如手机游戏,这就需要借助于Android2D绘图的支持.本篇博客主要讲解一下Android下使用Canvas ...
- nginx 反向代理 与 Apache backend的配置联合配置
nginx 反向代理 与 Apache backend的配置联合配置: 说明: nginx 将http映射到Apache上的特定子目录. 配置方法步骤: 1. 设置域名, 子域名映射到指定服务器ip ...
- Web app 的性能瓶颈与性能调优方法
1. web app 性能测试工具使用 2. mysql 性能分析与调优方法
- Updating Protobuf and GRPC in Golang
转自: http://russmatney.com/techsposure/update-protobuf-golang-grpc/ TL;DR: When protobuf updates, all ...
- Tomcat8配置数据库连接池
1.所有的tomcat项目共用一个连接池配置 1.1 修改conf->context.xml文件,在Context节点下配置 <Resource name="jdbc/myDat ...
- 支持取消操作和暂停操作的Backgroundworker
这也是今天讨论的话题.取消是默认就支持的,而暂停则默认不支持.但通过ManualResetEvent可以对其进行干预. using System; using System.Collections.G ...
- Cocos2dx 把 glview 渲染到 Qt 控件上(Mac 环境)
本文原链接:http://www.cnblogs.com/zouzf/p/4423256.html 环境:Mac 10.9.2 Xcode5.1.1 Qt5.3 cocos2dx-2.2.4 ...
- Java的wait(), notify()和notifyAll()使用小结
wait(),notify()和notifyAll()都是java.lang.Object的方法: wait(): Causes the current thread to wait until an ...
- Scala spark mongodb
最好的参考是Mongo官网的地址 https://docs.mongodb.com/spark-connector/getting-started/ 需要截图所示的包 代码地址 https://git ...