聯繫人根據名字的首個字符拼音分組(支持非法字符)

這裏用到了一個三方庫:PinYin4Objc
github地址:https://github.com/kimziv/PinYin4Objc

//處理聯繫人列表,包括按ABC...XYZ#順序排序並分組
//參數followList是聯繫人模型數組
//block中返回的參數list表示的是ABC...XYZ#數組,dict表示的是@{@"A":對應的聯繫人數組,@"B":...}
- (void)handlesectionTitleList:(NSArray *)followList resultBlock:(void(^)(NSDictionary *dict,NSArray *list)) resultBlock
{
    NSMutableDictionary *tempDic = [[NSMutableDictionary alloc]init];

    for(ContactsModel *contact  in followList)//ContactsModel:聯繫人模型
    {
        HanyuPinyinOutputFormat *formatter =  [[HanyuPinyinOutputFormat alloc] init];
        formatter.caseType = CaseTypeLowercase;
        formatter.vCharType = VCharTypeWithV;
        formatter.toneType = ToneTypeWithoutTone;

        if (contact.showName.length>0) {
            NSString *tempStr =[contact.showName substringToIndex:1];//列表中顯示的名字
            if ([tempStr isJudgeTheillegalCharacter]) {//判斷首字符是否是數字或是非法字符
                [tempDic setObject:contact forKey:@"#"];
            }else {
                NSString *outputPinyin=[PinyinHelper toHanyuPinyinStringWithNSString:contact.showName withHanyuPinyinOutputFormat:formatter withNSString:@""];
                [tempDic setObject:contact forKey:[[outputPinyin substringToIndex:1] uppercaseString]];
            }
        }
    }

    NSMutableArray *sectionTitleList = [[NSMutableArray alloc]initWithArray:tempDic.allKeys];
    NSMutableDictionary *nameDic = [NSMutableDictionary new];

    for (NSString *letter in sectionTitleList) {
        NSMutableArray *tempArry = [[NSMutableArray alloc] init];

        for (NSInteger i = 0; i<followList.count; i++) {
            ContactsModel *contact = followList[i];
            HanyuPinyinOutputFormat *formatter =  [[HanyuPinyinOutputFormat alloc] init];
            formatter.caseType = CaseTypeUppercase;
            formatter.vCharType = VCharTypeWithV;
            formatter.toneType = ToneTypeWithoutTone;

            if (contact.showName.length >0) {
                NSString *tempStr =[contact.showName substringToIndex:1];

                if ([letter isEqualToString:@"#"]) {
                    if ([tempStr isJudgeTheillegalCharacter]) {//判斷首字符是否是數字或是非法字符
                        [tempArry addObject:contact];

                    }
                }else {
                    //把friend的userName漢字轉爲漢語拼音,比如:張磊---->zhanglei
                    NSString *outputPinyin=[PinyinHelper toHanyuPinyinStringWithNSString:contact.showName withHanyuPinyinOutputFormat:formatter withNSString:@""];
                    if ([letter isEqualToString:[[outputPinyin substringToIndex:1] uppercaseString]]) {
                        [tempArry addObject:contact];

                    }
                }
            }
        }
        [nameDic setObject:tempArry forKey:letter];
    }

    sectionTitleList = [[NSMutableArray alloc]initWithArray:tempDic.allKeys];
    [sectionTitleList removeObject:@"#"];
    //排序,排序的根據是字母
    NSComparator cmptr = ^(id obj1, id obj2){
        if ([obj1 characterAtIndex:0] > [obj2 characterAtIndex:0]) {
            return (NSComparisonResult)NSOrderedDescending;
        }

        if ([obj1 characterAtIndex:0] < [obj2 characterAtIndex:0]) {
            return (NSComparisonResult)NSOrderedAscending;
        }
        return (NSComparisonResult)NSOrderedSame;
    };

    sectionTitleList = [[NSMutableArray alloc]initWithArray:[sectionTitleList sortedArrayUsingComparator:cmptr]];
    if ([[nameDic objectForKey:@"#"] count] >0) {
        [sectionTitleList addObject:@"#"];
    }

    if (resultBlock) {
        resultBlock(nameDic,sectionTitleList);
    }

}
//判斷是否含有非法字符 yes 有  no沒有
- (BOOL)isJudgeTheillegalCharacter{
    //提示 標籤不能輸入特殊字符
    NSString *str =@"^[A-Za-z\\u4e00-\u9fa5]+$";
    NSPredicate* emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    if (![emailTest evaluateWithObject:self]) {
        return YES;
    }
    return NO;
}

若有侵權,請聯繫我

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章