"recommended" iOS kanji to Pinyin third-party library
Source: Internet
Author: User
<span id="Label3"></p><p><p>PINYIN4OBJC is an open source library that supports both simplified and traditional Chinese in the git kanji pinyin. Efficiency Poapinyin and other libraries to high, the conversion library is complete below a brief introduction</p></p><p><p></p></p><p><p> <strong><span style="font-size: 18pt;">Implementation principle</span></strong></p></p><p><p>Use Unicode_to_hanyu_pinyin.txt to store Chinese character coding corresponding pinyin, loaded into memory in a dictionary</p></p><pre><span style="font-size: 14px;">NSString *resourcename =[[nsbundle mainbundle] pathforresource:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Unicode_to_hanyu_pinyin</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>OfType:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">txt</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; NSString</span></span>*dictionarytext=<span style="color: #000000;"><span style="color: #000000;">[nsstring stringwithcontentsoffile:resourcename encoding:nsutf8stringencoding error:nil]; Nsarray</span></span>*lines = [dictionarytext componentsseparatedbystring:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">\ r \ n</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; __block nsmutabledictionary</span></span>*tempmap=<span style="color: #000000;"><span style="color: #000000;">[[nsmutabledictionary alloc] init]; @autoreleasepool {[lines enumerateobjectsusingblock:</span></span>^ (id obj, nsuinteger idx, BOOL *<span style="color: #000000;"><span style="color: #000000;">STOP) {nsarray</span></span>*linecomponents=<span style="color: #000000;"><span style="color: #000000;">[obj Componentsseparatedbycharactersinset:[nscharacterset whitespacecharacterset]; [tempmap setobject:linecomponents[</span></span><span style="color: #800080;"><span style="color: #800080;">1</span></span>] Forkey:linecomponents[<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">]]; }]; }</span> self</span>->_unicodetohanyupinyintable=<span style="color: #000000;"><span style="color: #000000;">tempmap; [self cacheobjec:self</span></span>->_unicodetohanyupinyintable forkey:kcachekeyforunicode2pinyin];</span></pre><p><p></p></p><p><p></p></p><p><p>When we enter Chinese characters, we get the encoding of the Chinese Characters. The following figure is an Example. When we click the OK button</p></p><p><p></p></p><p><p></p></p><p><p>NSString *mainpinyinstrofchar = [pinyinhelper getfirsthanyupinyinstringwithchar:[str characterAtIndex:i] withhanyupinyinoutputformat:outputformat]; generate Chinese character coding through loops</p></p><p><p></p></p><p><p>Query from the dictionary for the value of key "8f6c" is</p></p><p><p></p></p><p><p>Take array the first one is pinyin ...</p></p><p><p></p></p><p><p> <strong><span style="font-size: 18pt;">Use</span></strong></p></p><p><p>1. Download the Library</p></p><p><p></p></p><p><p>2. Introduce the header file #import "PinYin4Objc.h"</p></p><p><p></p></p><p><p>3. Create phonetic character formats</p></p><p><p>Hanyupinyinoutputformat *outputformat=[[hanyupinyinoutputformat alloc] init];</p></p><p><p>Format contains Tonetype,chartype,casetype</p></p><pre><pre><span style="font-size: 14px;"><span style="color: #0000ff;">enum</span> <span style="color: #000000;">{ Tonetypewithtonenumber,//has A tonal number, such as PIN1 tonetypewithouttone,//no tone Tonetypewithtonemark}tonetype ;</span></span></pre></pre><pre><pre><span style="font-size: 14px;"><span style="color: #0000ff;">enum</span> <span style="color: #000000;">{ casetypeuppercase,//pinyin size casetypelowercase//pinyin lowercase}casetype;</span></span></pre></pre><pre><pre><span style="font-size: 14px;"><span style="color: #0000ff;">enum</span> <span style="color: #000000;">{ vchartypewithuandcolon, vchartypewithv, vchartypewithuunicode}vchartype;</span></span></pre></pre><p><p></p></p><p><p></p></p><p><p>4. Formatting</p></p><p><p>[outputformat settonetype:tonetypewithouttone];</p></p><p><p>[outputformat setvchartype:vchartypewithv];</p></p><p><p>[outputformat setcasetype:casetypelowercase];</p></p><p><p></p></p><p><p>5. Call ToHanyuPinyinStringWithNSString:withHanyuPinyinOutputFormat:withNSString:outputBlock to convert</p></p><pre><pre>[pinyinhelper Tohanyupinyinstringwithnsstring:sourcetext Withhanyupinyinoutputformat:outputformat withNSString: <span style="color: #800000;">@"</span> <span style="color: #800000;">"</span> outputblock:^ (nsstring *<span style="color: #000000;">pinYin) { _outputtv.text</span>=<span style="color: #000000;">pinYin; }];</span></pre></pre><p><p></p></p><p><p>"recommended" iOS kanji to Pinyin third-party library</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service