iOS_中国汉字到拼音_pinyin4objc
最后效果图:
ViewController.h
//
// ViewController.h
// PinYin4Objc汉字转拼音演示demo
//
// Created by beyond on 14-7-26.
// Copyright (c) 2014年 com.beyond. All rights reserved.
// #import <UIKit/UIKit.h> @interface ViewController : UIViewController
// 输入框
@property (nonatomic,weak) IBOutlet UITextField *textFieldInput;
// 显示用时多少
@property (nonatomic,weak) IBOutlet UILabel *labelTime;
// 显示字数多少
@property (nonatomic,weak) IBOutlet UILabel *labelWordsCount;
// 结果显示框
@property (nonatomic,weak) IBOutlet UITextView *textFieldResult; // 转换button
@property (nonatomic,weak) IBOutlet UIButton *btnOK;
- (IBAction)btnOKClick:(id)sender;
@end
ViewController.m
//
// ViewController.m
// PinYin4Objc汉字转拼音演示demo
//
// Created by beyond on 14-7-26.
// Copyright (c) 2014年 com.beyond. All rights reserved. /*
PinYin4Objc 是一个流行的汉字(支持简体和繁体)转拼音的objc库,有下面特性:
1.效率高,使用数据缓存。第一次初始化以后,拼音数据存入文件缓存和内存缓存。后面转换效率大大提高;
2.支持自己定义格式化。拼音大写和小写等等;
3.拼音数据完整,支持简体中文和繁体,与网络上流行的相关项目比,数据非常全,差点儿没有出现转换错误的问题。 PinYin4Objc is a popular objective-c library supporting convertion between Chinese(both Simplified and Tranditional) characters and most popular Pinyin systems, it's performance is very efficient, data cached at first time. The output format of pinyin could be customized. 性能比較:
与之前的pinyin。POAPinyin和PYMethod等项目比較,PinYin4Objc的速度是非常快的 声调格式化。比如:“刘”字的格式化后为“liu2”或“liu”或“liú” 对特殊拼音ü的的显示格式。 比如“u:”或“v”或“ü” 大写和小写的转换。比如:“liu2”或“LIU2” 设置声调格式:
outputFormat.setToneType(HanyuPinyinToneType);
參数HanyuPinyinToneType有下面常量对象: HanyuPinyinToneType.WITH_TONE_NUMBER 用数字表示声调,比如:liu2 HanyuPinyinToneType.WITHOUT_TONE 无声调表示,比如:liu HanyuPinyinToneType.WITH_TONE_MARK 用声调符号表示。比如:liú 设置特殊拼音ü的显示格式: outputFormat.setVCharType(HanyuPinyinVCharType); 參数HanyuPinyinVCharType有下面常量对象: HanyuPinyinVCharType.WITH_U_AND_COLON 以U和一个冒号表示该拼音。比如:lu: HanyuPinyinVCharType.WITH_V 以V表示该字符,比如:lv HanyuPinyinVCharType.WITH_U_UNICODE 以ü表示 */ #import "ViewController.h"
#import "PinYin4Objc.h"
@interface ViewController () @end @implementation ViewController // 点击button,转换汉字为拼音
- (IBAction)btnOKClick:(id)sender
{
// 1,获取输入的文本
NSString *inputText=_textFieldInput.text;
// robust推断
if ([inputText isEqual:@""]) {
_labelTime.text= @"";
_labelWordsCount.text = @"";
_textFieldResult.text = @"";
// 输入框活的焦点
[_textFieldInput becomeFirstResponder];
return;
}
// 2,实例化拼音格式化对象
HanyuPinyinOutputFormat *outputFormat=[[HanyuPinyinOutputFormat alloc] init];
// 3,设置拼音格式化对象的參数------重点
/*
typedef enum {
ToneTypeWithToneNumber,数字1234表示声调
ToneTypeWithoutTone, 没有声调
ToneTypeWithToneMark 标记声调 这个用不了
}ToneType; typedef enum {
VCharTypeWithUAndColon,
VCharTypeWithV,
VCharTypeWithUUnicode
}VCharType;
*/
// 声调就是没有声调
// [outputFormat setToneType:ToneTypeWithoutTone];
// 声调是数字,跟在后面 海阔天空 <-----> hai3 kuo4 tian1 kong1
[outputFormat setToneType:ToneTypeWithToneNumber];
// 标记声调的这个用不了...
// [outputFormat setToneType:ToneTypeWithToneMark]; // V的表示方法:
[outputFormat setVCharType:VCharTypeWithV];
// V的表示方法:
// [outputFormat setVCharType:VCharTypeWithUUnicode];
// V的表示方法:
// [outputFormat setVCharType:VCharTypeWithUAndColon]; // 结果是大写还是小写,一般小写
[outputFormat setCaseType:CaseTypeLowercase];
// 4,记录转换的開始时间
NSTimeInterval startTime=[[NSDate date] timeIntervalSince1970];
// 5,拼音工具类的类方法转换汉字为拼音,參数1:输入的汉字,參数2:格式化器,參数3:seperator分隔符
NSString *outputPinyin=[PinyinHelper toHanyuPinyinStringWithNSString:inputText withHanyuPinyinOutputFormat:outputFormat withNSString:@" "];
// 6,记录转换的结束时间
NSTimeInterval endTime=[[NSDate date] timeIntervalSince1970];
// 7,计算用时
NSTimeInterval totalTime=endTime-startTime;
// 8,回显结果到界面
_labelTime.text=[NSString stringWithFormat:@"共用时:%fs",totalTime];
_labelWordsCount.text=[NSString stringWithFormat:@"字符数:%i 个",inputText.length];
_textFieldResult.text=outputPinyin;
NSLog(@"%@",outputPinyin);
// 转换为秒
// NSLog(@"seconds:%f",(endTime - startTime)/(float)CLOCKS_PER_SEC); // 调用自己定义方法,退出键盘
[self exitKeyboard]; }
// 自己定义方法,退出键盘
- (void)exitKeyboard
{
// 退出键盘
// 方式1: self.view内部全部的文本框(包含子孙控件...)都退出第一响应者
[self.view endEditing:YES];
return; // 方式2:
// 遍历uiview里面全部的控件 ,resignFirstResponder
for (UIView *obj in self.view.subviews) {
if ([obj isKindOfClass:[UITextField class]]) {
[obj resignFirstResponder];
}
}
// 方式3:
// 在self.view的最开头,铺一个全屏的透明的button,连线,仅仅要在屏幕空白区域点击后,就能够调用上面的方式1,退出键盘
} @end
storyboard截图:
版权声明:本文博主原创文章,博客,未经同意不得转载。
iOS_中国汉字到拼音_pinyin4objc的更多相关文章
- 【转】Python 爬虫的工具列表【预】
这个列表包含与网页抓取和数据处理的Python库 网络 通用 urllib -网络库(stdlib). requests -网络库. grab – 网络库(基于pycurl). pycurl – 网络 ...
- 洗礼灵魂,修炼python(52)--爬虫篇—【转载】爬虫工具列表
与爬虫相关的常用模块列表. 原文出处:传送门链接 网络 通用 urllib -网络库(stdlib). requests -网络库. grab – 网络库(基于pycurl). pycurl – 网络 ...
- Python 爬虫的工具列表 附Github代码下载链接
Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...
- 转--Python标准库之一句话概括
作者原文链接 想掌握Python标准库,读它的官方文档很重要.本文并非此文档的复制版,而是对每一个库的一句话概括以及它的主要函数,由此用什么库心里就会有数了. 文本处理 string: 提供了字符集: ...
- python 爬虫第三方库
这个列表包含与网页抓取和数据处理的Python库 网络 通用 urllib -网络库(stdlib). requests -网络库. grab – 网络库(基于pycurl). pycurl – 网络 ...
- Python 爬虫的工具列表大全
Python 爬虫的工具列表大全 这个列表包含与网页抓取和数据处理的Python库.网络 通用 urllib -网络库(stdlib). requests -网络库. grab – 网络库(基于pyc ...
- python 进阶(转自http://python.jobbole.com/82633/)
网络 通用 urllib -网络库(stdlib). requests -网络库. grab – 网络库(基于pycurl). pycurl – 网络库(绑定libcurl). urllib3 – P ...
- Python 爬虫的工具列表
这个列表包含与网页抓取和数据处理的Python库 网络 通用 urllib -网络库(stdlib). requests -网络库. grab - 网络库(基于pycurl). pycurl - 网络 ...
- [转] Python 爬虫的工具列表 附Github代码下载链接
转自http://www.36dsj.com/archives/36417 这个列表包含与网页抓取和数据处理的Python库 网络 通用 urllib -网络库(stdlib). requests - ...
随机推荐
- mfc 链接时错误 文件函数重复定义
我在HeaderFile里新建了一个函数,然后在程序里调用,一直出现这个错误,说这个函数重复定义, 发现是VS自动加到External dependencies里面了.把HeaderFile里的函数文 ...
- 有关XCode6(iOS8)UITableViewCell与iOS7在UITableViewCell问题
简而言之: iOS6在cell的层次关系2层,但在iOS7层次结构成为3层,但在iOS8的SDK在UITableViewCell层次结构发生了变化2层. 如果它们是UITableViewCell加入到 ...
- Sizzle.filter [ 源代码分析 ]
最近的研究已Sizzle选择,对于原理中我们也不得不佩服! Sizzle中间filter办法.主要负责元素表达式过滤块的集合,在内部的方法调用Sizzle.selector.fitler滤波操作的操作 ...
- 走向DBA[MSSQL篇] 针对大表 设计高效的存储过程【原理篇】 附最差性能sql语句进化过程客串
原文:走向DBA[MSSQL篇] 针对大表 设计高效的存储过程[原理篇] 附最差性能sql语句进化过程客串 测试的结果在此处 本篇详解一下原理 设计背景 由于历史原因,线上库环境数据量及其庞大,很多千 ...
- Instll meld in windows
在linux下用meld感觉颇为不错,但是在windows上真是折腾老久.p4merge也试了试, 但还是meld用起来更顺手. 使用 https://wiki.gnome.org/Meld/Wind ...
- atitit.提升稳定性---hibernate 添加重试retry 机制解决数据库连接关闭
atitit.提升稳定性---hibernate 添加重试retry 机制解决数据库连接关闭 1. 流程总结 retry(5times).invoke(xxx).test().rest().$() t ...
- 自动注册 IIS6 的 MIME 类型
原文 自动注册 IIS6 的 MIME 类型 由于IIS5和IIS6有很多的MIME类型没有设置,其中还包括了FLV(video/x-flv),上篇文章描述了制作<IIS6 自动安装>,而 ...
- Tomcat通过JNDI方式链接MySql数据库
原文:Tomcat通过JNDI方式链接MySql数据库 拷贝MySQL的JDBC驱动到Tomcat的lib路径下 配置全局数据源或者单个Web应用的局部数据源 局部数据源 在Tomcat的conf/C ...
- 手游client思考框架
手游新公司新项目client我不太同意框架.虽然我也终于让步,当他居然问老板,使这个幼稚的行为而悔恨. 然而,就在最近我写了一些代码视图,我更坚定了自己的想法和思想.和思路不一定适合其它人,所以我并不 ...
- ORA-12638: 无法检索身份证明 解决的方法
the NTS option makes the Oracle client attempt to use your current Windows domain credentials to aut ...