iOS全角符与半角符之间的转换

时间:2021-10-08 14:41:31

iOS全角符与半角符之间的转换

相关资料:

问题

  • 1、17-03-15,「有人在群里边问怎么把『半角』符字符串转换成『全角』字符串?」,百度的结果如下:

    • 全角转半角方法如下(略有出入,原文使用的是『MRC』,现在使用的『ARC』):

      NSString * string = @"abcdefg,";
      
      NSMutableString *convertedString = [string mutableCopy];
      
      CFStringTransform((CFMutableStringRef)convertedString, NULL, kCFStringTransformFullwidthHalfwidth, false);
      
      NSLog(@"ddc:%@",convertedString); 
      • 输出结果:ddc:abcdefg,

      • 结果转换成功。

    • 半角转全角只需要把 kCFStringTransformFullwidthHalfwidth 换成kCFStringTransformHiraganaKatakana 即可。

      NSString * string = @"abcdefg,";
      
      NSMutableString *convertedString = [string mutableCopy];
      
      CFStringTransform((CFMutableStringRef)convertedString, NULL, kCFStringTransformHiraganaKatakana, false);
      
      NSLog(@"ddc:%@",convertedString);
      • 输出结果:ddc:abcdefg,

      • 结果转换失败。

解决

  • 1、首先来看一下函数『CFString​Transform』的说明:

    正如它的 CF 前缀所表述的一样,CFStringTransform 是 Core Foundation 中的一部分。这个函数传入以下参数,并返回一个 Boolean 来表示转换是否成功:
    • string: 需要转换的字符串。由于这个参数是 CFMutableStringRef 类型,一个 NSMutableString 类型也可以通过*桥接的方式传入。
    • range: 转换操作作用的范围。这个参数是 CFRange,而不是 NSRange。
    • transform: 需要应用的变换。这个参数使用了包含下面将提到的字符串常量的 ICU transform string。
    • reverse: 如有需要,是否返回反转过的变换。
  • 2、我们来看一下『Transform identifiers for CFStringTransform』:

    k​CFString​Transform​Strip​Combining​Marks
    The identifier of a transform to strip combining marks (accents or diacritics).
    
    k​CFString​Transform​To​Latin
    The identifier of a transform to transliterate all text possible to Latin script. Ideographs are transliterated as Mandarin Chinese.
    
    k​CFString​Transform​Fullwidth​Halfwidth
    The identifier of a reversible transform to convert full-width characters to their half-width equivalents.
    
    k​CFString​Transform​Latin​Katakana
    The identifier of a reversible transform to transliterate text to Katakana from Latin.
    
    k​CFString​Transform​Latin​Hiragana
    The identifier of a reversible transform to transliterate text to Hiragana from Latin.
    
    k​CFString​Transform​Hiragana​Katakana
    The identifier of a reversible transform to transliterate text to Katakana from Hiragana.
    
    k​CFString​Transform​Mandarin​Latin
    The identifier of a transform to transliterate text to Latin from ideographs interpreted as Mandarin Chinese. This transform is not reversible.
    
    k​CFString​Transform​Latin​Hangul
    The identifier of a reversible transform to transliterate text to Hangul from Latin.
    
    k​CFString​Transform​Latin​Arabic
    The identifier of a reversible transform to transliterate text to Arabic from Latin.
    
    k​CFString​Transform​Latin​Hebrew
    The identifier of a reversible transform to transliterate text to Hebrew from Latin.
    
    k​CFString​Transform​Latin​Thai
    The identifier of a reversible transform to transliterate text to Thai from Latin.
    
    k​CFString​Transform​Latin​Cyrillic
    The identifier of a reversible transform to transliterate text to Cyrillic from Latin.
    
    k​CFString​Transform​Latin​Greek
    The identifier of a reversible transform to transliterate text to Greek from Latin.
    
    k​CFString​Transform​To​XMLHex
    The identifier of a reversible transform to transliterate characters other than printable ASCII to XML/HTML numeric entities.
    
    k​CFString​Transform​To​Unicode​Name
    The identifier of a reversible transform to transliterate characters other than printable ASCII (minus braces) to their Unicode character name in braces.
    
    k​CFString​Transform​Strip​Diacritics
    The identifier of a transform to remove diacritic markings.  

    具体的自己翻译哈啊(附表一部分翻译结果 来源):

    Transformation Input Output
    kCFStringTransformLatinArabic mrḥbạ مرحبا
    kCFStringTransformLatinCyrillic privet привет
    kCFStringTransformLatinGreek geiá sou γειά σου
    kCFStringTransformLatinHangul annyeonghaseyo 안녕하세요
    kCFStringTransformLatinHebrew şlwm שלום
    kCFStringTransformLatinHiragana hiragana ひらがな
    kCFStringTransformLatinKatakana katakana カタカナ
    kCFStringTransformLatinThai s̄wạs̄dī สวัสดี
    kCFStringTransformHiraganaKatakana にほんご ニホンゴ
    kCFStringTransformMandarinLatin 中文 zhōng wén
  • 3、于是乎 kCFStringTransformHiraganaKatakana 实际上是『片假名』与『平假名』之间的转换,『CFString​Transform』函数中的 reverse 参数意思是转换过程是否可逆;

    NSString * string = @"abcdefg,";
    
    NSMutableString *convertedString = [string mutableCopy];
    
    CFStringTransform((CFMutableStringRef)convertedString, NULL, kCFStringTransformFullwidthHalfwidth, true);
    
    NSLog(@"ddc:%@",convertedString);  
    • 输出结果:ddc:abcdefg,

    • 结果转换成功。

彩蛋

  • 请尝试下列代码:

    NSString * string = @"我是中文";
    
    NSMutableString *convertedString = [string mutableCopy];
    
    CFStringTransform((CFMutableStringRef)convertedString, NULL, kCFStringTransformMandarinLatin, false);
    
    NSLog(@"转换结果:%@",convertedString);
    
    CFStringTransform((CFMutableStringRef)convertedString, NULL, kCFStringTransformStripDiacritics, false);
    
    NSLog(@"转换结果:%@",convertedString);

备注

  • 请注意不是所有的『Transform identifiers』都支持逆转