1. Need to use third-party library PINYIN4OBJC2. Use method 2.1 to set the output format
Hanyupinyinoutputformat *fmt = [[Hanyupinyinoutputformat alloc]init];
Fmt.casetype = Casetypeuppercase; Indicates output by capital letter
Fmt.tonetype = Tonetypewithouttone; It means no tone.
Fmt.vchartype = Vchartypewithuunicode; V to u indicates
2.2 Performing conversions
NSString *pinyin = [pinyinhelper tohanyupinyinstringwithnsstring:@ "Beijing" withhanyupinyinoutputformat:fmt withNSString: @"#"];
Parameter 1:@ "Beijing", Chinese string to be converted
Parameter 2:fmt, output format
Parameter 3:@ "#", delimiter, can be an empty string @ ""
Result of printing string pinyin: bei#jing
3. Achieve the first letter of Pinyin search
NSString *pinyin2 = [Pinyinhelper toHanyuPinyinStringWithNSString:city.name withhanyupinyinoutputformat:fmt withnsstring:@ "#";
Nsarray *words = [pinyin2 componentsseparatedbystring:@ "#"];
nsmutablestring *headletterstr = [nsmutablestring string];
[Words enumerateobjectsusingblock:^ (id obj, Nsuinteger idx, BOOL *stop) {
[Headletterstr appendstring:[(NSString *) obj substringtoindex:1]];
}];
4. General search criteria, cite examples of urban search
if (([city.name rangeofstring:condition].length! = 0) | |
([pinyin rangeofstring:condition.uppercasestring].length! = 0) | |
([headletterstr Rangeofstring:condition.uppercasestring].length! = 0)
)
{
Search success, need to do things
}
5. Actual effect
Search for a phonetic search of IOS