前言
iOS处理语言工具CFStringTransform :智能地处理用户的输入内容,经典应用场景【索引】
- 原理:通过对用户输入内容,利用
CFStringTransform
变换,可以轻松实现实现一个通用的搜索index - 从CSDN下载通讯录demo源码
https://download.csdn.net/download/u011018979/19088189
特色:搜索内容可以是多语言的
I、 CFStringTransform
1.1 初识
很明显CF
StringTransform 是 Core Foundation 中的一部分。
/* Perform string transliteration. The transformation represented by transform is applied to the given range of string, modifying it in place. Only the specified range will be modified, but the transform may look at portions of the string outside that range for context. NULL range pointer causes the whole string to be transformed. On return, range is modified to reflect the new range corresponding to the original range. reverse indicates that the inverse transform should be used instead, if it exists. If the transform is successful, true is returned; if unsuccessful, false. Reasons for the transform being unsuccessful include an invalid transform identifier, or attempting to reverse an irreversible transform. You can pass one of the predefined transforms below, or any valid ICU transform ID as defined in the ICU User Guide. Note that we do not support arbitrary set of ICU transform rules. */ CF_EXPORT Boolean CFStringTransform(CFMutableStringRef string, CFRange *range, CFStringRef transform, Boolean reverse);
第一个参数CFMutableStringRef:要转换的字符串 第二个参数CFRange 作用范围:NULL,视为全部转换 第三个参数 Transform identifiers->指定要进行什么样的转换 第四个参数reverse:是否采用可逆变换
接下来重点讲解下Transform identifiers。
1.2 Transform identifiers
- 不同拼写之间的转换
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 |
kCFStringTransformToLatin 非英文文本转换为拉丁字母
- 去掉重音和变音符号
kCFStringTransformStripCombiningMarks
- 找出特殊字符的 Unicode 标准名
kCFStringTransformToUnicodeName
包括 Emoji
”🐑💨✨” 转换成 “{SHEEP} {DASH SYMBOL} {SPARKLES}”, “🐷” -> “{PIG FACE}”。
II、例子:通讯录索引
2.1 原理
- (NSString *)pinyin{ NSMutableString *str = [self mutableCopy]; CFStringTransform(( CFMutableStringRef)str, NULL, kCFStringTransformMandarinLatin, NO);//先转换为带声调的拼音 CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformStripDiacritics, NO);//去掉音标 return [[str stringByReplacingOccurrencesOfString:@" " withString:@""] lowercaseString];//小写 }
2.2 demo
- 从CSDN下载恶魔源码
https://download.csdn.net/download/u011018979/19088189private:https://github.com/zhangkn/StringTransform4Index
- 原理:通过对用户输入内容,利用
CFStringTransform
变换,可以轻松实现实现一个通用的搜索index- 特色:搜索内容可以是多语言的