zoukankan      html  css  js  c++  java
  • 【推荐】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:@"
    "];
            __block NSMutableDictionary *tempMap=[[NSMutableDictionary alloc] init];
            @autoreleasepool {
                    [lines enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                        NSArray *lineComponents=[obj componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
                        [tempMap setObject:lineComponents[1] forKey:lineComponents[0]];
                    }];
             }
            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;
    
      }];
  • 相关阅读:
    jwt 0.9.0(三)jwt客户端存储状态可行性分析,及Java代码案例
    jwt 0.9.0(二)jwt官网资料总结
    jwt 0.9.0(一)推荐jwt理由
    jwt 0.9.0 系列目录
    Elasticsearch-6.7.0系列(八)开启kibana监控
    Elasticsearch-6.7.0系列(七)SpringCloud连接ES集群,使用ES用户名密码
    Elasticsearch-6.7.0系列(六)ES设置集群密码
    Oracle 11g Java驱动包ojdbc6.jar安装到maven库,并查看jar具体版本号
    nginx1.14.0版本location路径,多级文件目录配置,root与alias的配置区别
    docker18.09.5 Dockerfile文件编写
  • 原文地址:https://www.cnblogs.com/salam/p/5126053.html
Copyright © 2011-2022 走看看