iOS_漢字轉拼音_pinyin4objc,ios_pinyin4objc

來源:互聯網
上載者:User

iOS_漢字轉拼音_pinyin4objc,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;// 轉換按鈕@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// 點擊按鈕,轉換漢字為拼音- (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的最開頭,鋪一個全屏的透明的按鈕,連線,只要在螢幕空白地區點擊後,就可以調用上面的方式1,退出鍵盤}@end








storyboard:







想找個拼音轉為漢字的軟體注意是pinyin轉為為漢字

現在好像只有網可以轉,大概還沒有這樣的軟體。
 
怎將拼音轉換漢字

問題很抽象,沒有具體補充嗎?在電腦上你可以轉換一下IME,過去常用的拼音輸入智能ABC,現在常用的搜狗IME,都可以將拼音轉換成漢字,只要你輸入相應的拼音,然後按數字選擇你要打的字就可以了。若是手機的話,因手機而異,不好細說,但是都不會很難,可以找親近的人研究一下
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.