iOS_中國漢字到拼音_pinyin4objc

來源:互聯網
上載者:User

標籤:

最後:





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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.