iOS高阶UI相关iOS开发牛叉的demo

ios 一个方法实现通讯录搜索,支持拼音,汉子,拼音首字母,拼音

2017-08-14  本文已影响235人  为之则易ing

效果:

searchTest1.gif xly_search.gif

使用:只需一个方法

    [self.search searchWithSearchString:searchText andModeDataArray:_dataArray andSearchPropertys:@[@"name",@"phone"] complete:^(NSMutableArray *resultData,BOOL search) {
        [_searchArray removeAllObjects];
        [_searchArray addObjectsFromArray:resultData];
        [_tabelV reloadData];
    } sort:^BOOL(id o1, id o2) {//用于排序
        return [o1 searchStringRange_ly].location > [o2 searchStringRange_ly].location;
    }];

内部实现:
1、汉子转拼音(使用第三方:PinYin4Objc,系统效率低)

-(NSString *)pinyinString{
    if (self && self.length) {
        HanyuPinyinOutputFormat *outputFormat=[[HanyuPinyinOutputFormat alloc] init];
        [outputFormat setToneType:ToneTypeWithoutTone];
        [outputFormat setVCharType:VCharTypeWithUAndColon];
        [outputFormat setCaseType:CaseTypeLowercase];
        
        NSString *pinyinStr = [[PinyinHelper toHanyuPinyinStringWithNSString:self withHanyuPinyinOutputFormat:outputFormat withNSString:@""] lowercaseString] ? : @"";
        return pinyinStr;
        /*效率低
         
        if ([self stringByReplacingOccurrencesOfString:@" " withString:@""].length == 0) {
            return nil;
        }
        NSMutableString *pinyin = [self mutableCopy];
        CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO);
        CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO);
        return [[pinyin stringByReplacingOccurrencesOfString:@" " withString:@""] lowercaseString];
         */
    }else{
        return @"";
    }
}

2、匹配核心代码(包含拼音、拼音首字母、汉子、汉子拼音的匹配)

-(NSRange)rangeOfSearchString:(NSString *)searchString andChineseMatchType:(LYSearchType)searchType{
    if (self == nil || searchString == nil) {
        return NSMakeRange(0, 0);
    }
    if (((searchType & LYSearchWithChineseExact) || (searchType & LYSearchWithChineseChineseAndPinyin)) && [searchString containChinese]) {
        NSRange ran = [self rangeOfString:searchString];
        if (ran.length) {
            return ran;
        }else{
            if (searchType & LYSearchWithChineseChineseAndPinyin) {
                if ([searchString isChinese] == NO) {
                    NSRange ra = [self rangeOfSearchString:searchString andChineseMatchType:LYSearchWithChinesePinyin];
                    if (ra.length) {
                        NSString *subString = [self substringWithRange:ra];
                        
                        NSMutableString *chineseStr = [NSMutableString string];
                        NSMutableString *searchChineseStr = [NSMutableString string];
                        
                        for (int i = 0; i < subString.length; i ++) {
                            NSString *str = [subString substringWithRange:NSMakeRange(i, 1)];
                            if ([str isChinese]) {
                                [chineseStr appendString:str];
                            }
                        }
                        
                        for (int i = 0; i < searchString.length; i ++) {
                            NSString *str = [searchString substringWithRange:NSMakeRange(i, 1)];
                            if ([str isChinese]) {
                                [searchChineseStr appendString:str];
                            }
                        }
                        
                        if (chineseStr.length && searchChineseStr.length && [chineseStr rangeOfString:searchChineseStr].length) {
                            return ra;
                        }
                    }
                }
            }
        }
        return NSMakeRange(0, 0);
    }

    
    
    if (searchType & LYSearchWithChinesePinyin) {
        NSString *searchPy = [searchString pinyinString];
        NSString *pyStr = [self pinyinString];
        if (searchPy == nil || pyStr == nil) {
            return NSMakeRange(0, 0);
        }
        NSRange range = [pyStr rangeOfString:[searchPy lowercaseString]];
        if (range.length) {
            NSRange returnrange = NSMakeRange(0, 0);
            BOOL has = NO;
            for (int i = 0; i < self.length; i ++) {
                NSString *str = [self substringWithRange:NSMakeRange(i, 1)];
                NSString *py = [str pinyinString];
                if (py == nil) {
                    return NSMakeRange(0, 0);
                }
                NSRange ra = [pyStr rangeOfString:py];
                if (ra.location == range.location) {
                    returnrange.location = i;
                    has = YES;
                    break;
                }else if (ra.location > range.location){
                    //                index = i - 1;
                    break;
                }
            }
            for (int i = 1; i < self.length - returnrange.location + 1 && has; i ++) {
                NSString *subStr = [self substringWithRange:NSMakeRange(returnrange.location,i)];
                NSString *subStrPy = [subStr pinyinString];
                if (subStrPy == nil) {
                    return NSMakeRange(0, 0);
                }
                if ([subStrPy rangeOfString:[searchPy lowercaseString]].length) {
                    returnrange.length = i;
                    break;
                }
            }
            if (returnrange.length == 0) {
                if (searchType & LYSearchWithChineseInitials && [searchString containChinese] == NO) {
                    NSString *searchPy = [searchString pinyinString];
                    NSString *firstCharString = [self firstCharsString];
                    if (firstCharString == nil) {
                        return NSMakeRange(0, 0);
                    }
                    NSRange range = [firstCharString rangeOfString:searchPy];
                    return range;
                }
            }

            
            return returnrange;
        }else{
            if (searchType & LYSearchWithChineseInitials) {
                NSString *firstCharString = [self firstCharsString];
                if (firstCharString == nil) {
                    return NSMakeRange(0, 0);
                }
                NSRange range = [firstCharString rangeOfString:searchPy];
                return range;

            }
        }
    }else{
        if (searchType & LYSearchWithChineseInitials && [searchString containChinese] == NO) {
            NSString *searchPy = [searchString pinyinString];
            NSString *firstCharString = [self firstCharsString];
            if (firstCharString == nil) {
                return NSMakeRange(0, 0);
            }
            NSRange range = [firstCharString rangeOfString:searchPy];
            return range;
            
        }
    }
    return NSMakeRange(0, 0);
}
-(NSString *)stringOfSearchString:(NSString *)searchString andChineseMatchType:(LYSearchType)searchType{
    NSRange range = [self rangeOfSearchString:searchString andChineseMatchType:searchType];
    if (range.length) {
        return [self substringWithRange:range];
    }
    return nil;
}

git:https://github.com/GitHubXuLiying/Search

上一篇下一篇

猜你喜欢

热点阅读