[IOS]——模糊搜索的实现

模糊搜索的实现(支持拼音搜索,汉字搜索,首字母搜索)

搜索是要用到UISearchBar

我做出的UISearchBar的效果

UISearchBar的代码实现

1.//首先要遵守协议
@interface weiboViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>
2.//代码的实现
//搜索框
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sousuo"] style:0 target:self action:@selector(setUpsearch)];
    _search = [[UISearchBar alloc] init];
    //显示搜索框的cancel按钮
    _search.showsCancelButton = YES;
    //设置代理
    _search.delegate = self;
    //[UIApplication sharedApplication].statusBarFrame.size.height这是获取状态栏的高度
    //self.navigationController.navigationBar.frame.size.height这是获取导航栏的高度
    _search.frame = CGRectMake(0,[UIApplication sharedApplication].statusBarFrame.size.height+self.navigationController.navigationBar.frame.size.height-35, [UIScreen mainScreen].bounds.size.width, 35);
    //把搜索框隐藏在navigationBar的后面
    [self.navigationController.view insertSubview:_search belowSubview:self.navigationController.navigationBar];
    //动画下拉搜索框的关键代码
    //因为一开始搜索框是隐藏在navigationBar背后的,当时距离底部为35,那么我设置search下滑35就能完全地将search显示出来
    [UIView animateWithDuration:1.0 animations:^{
        self.search.transform = CGAffineTransformMakeTranslation(0, 35);
    }];

重点来了!模糊搜索的实现(我已经将方法封装好,可以直接复制去测试!)

这是将搜索范围内的文本全部转成不带声调的拼音

//将汉字转变为拼音
- (NSString *)transformToPinyin:(NSString *)searchtext
{
    //转成了可变字符串
    NSMutableString *str = [NSMutableString stringWithString:searchtext];
    //将汉字转变为拼音
    CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformMandarinLatin,NO);
    //再转换为不带声调的拼音
    CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformStripDiacritics,NO);
    //转变后的str每个字是旁边是有空格分开的
    NSArray *pinyinArray = [str componentsSeparatedByString:@" "];
    NSMutableString *allString = [NSMutableString new];
    int count = 0;
    //这是把汉字的拼音全部拼接起来
    for (int  i = 0; i < pinyinArray.count; i++)
    {
        for(int i = 0; i < pinyinArray.count;i++)
        {
            if (i == count) {
                [allString appendString:@"#"];
                //区分第几个字母
            }
            [allString appendFormat:@"%@",pinyinArray[i]];
        }
        [allString appendString:@","];
        count ++;
    }
    NSMutableString *initialStr = [NSMutableString new];
    //这是把汉字的首字母拼接起来
    for (NSString *s in pinyinArray)
    {
        if (s.length > 0)
        {
            [initialStr appendString:  [s substringToIndex:1]];
        }
    }
    [allString appendFormat:@"#%@",initialStr];
    [allString appendFormat:@",#%@",searchtext];
    return allString;
}

由于上面方法是有局限性的,因为把搜索范围内的文本都转成的拼音都是小写的,因此我再提供一个方法来将你在搜索框输入的大写字母都转成小写字母

-(NSString *)changetoLower:(NSString *)str
{
    for (NSInteger i=0; i<str.length; i++) {
        if ([str characterAtIndex:i]>='A'&&[str characterAtIndex:i]<='Z') {
            char  temp=[str characterAtIndex:i]+32;
            NSRange range=NSMakeRange(i, 1);
            str=[str stringByReplacingCharactersInRange:range withString:[NSString stringWithFormat:@"%c",temp]];
        }
    }
    return str;
}

你可能感兴趣的:(IOS,日记,模糊搜索的实现)