文章目录
- 前言
- I、 CFStringTransform 初识
-
- 1.1.1 不同拼写之间的转换
- 1.1.2 去掉重音和变音符号
- 1.1.3 找出特殊字符的 Unicode 标准名
- 1.1 Transform identifiers
- II、例子:通讯录索引
前言
通过对用户输入内容,利用CFStringTransform
变换,可以轻松实现实现一个通用的搜索index
搜索内容可以是多语言的
I、 CFStringTransform 初识
很明显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.1 Transform identifiers
1.1.1 不同拼写之间的转换
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 非英文文本转换为拉丁字母
1.1.2 去掉重音和变音符号
kCFStringTransformStripCombiningMarks
1.1.3 找出特殊字符的 Unicode 标准名
kCFStringTransformToUnicodeName
包括 Emoji
”??✨” 转换成 “{SHEEP} {DASH SYMBOL} {SPARKLES}”, “?” -> “{PIG FACE}”。
II、例子:通讯录索引
- StringTransform4Index源码
- (NSString *)pinyin{
NSMutableString *str = [self mutableCopy];
CFStringTransform(( CFMutableStringRef)str, NULL, kCFStringTransformMandarinLatin, NO);//先转换为带声调的拼音
CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformStripDiacritics, NO);//去掉音标
return [[str stringByReplacingOccurrencesOfString:@" " withString:@""] lowercaseString];//小写
}
# see also
想要源码,请点击https://kunnan.blog.csdn.net/article/details/109603377