微信消息搜索 只显示关键字

最近产品提了一个很无奈的需要,显示某一段文字中的一句,而且高亮显示关键字。

额,好吧。

开始以为微信用了某个iOS SDK现有功能,想了好半天,想不出。等晚上回家,连接上Reveal,看了一眼,我去,微信竟然将前后的字符都给截掉了,只保留了需要显示部分。好吧,那我也这么做

写在了UILabel的分类的里面
直接使用:

  • (NSAttributedString *)keyLineWithKeyword:(NSString *)keyword
    就好了。
IMG_2423.PNG
2293362-aaa31a1a605f2675.png
- (NSAttributedString *)keyLineWithKeyword:(NSString *)keyword
{
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    paragraph.alignment = NSTextAlignmentLeft;//设置对齐方式
    paragraph.lineBreakMode = NSLineBreakByCharWrapping;
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:self.text attributes:@{NSFontAttributeName : self.font,NSForegroundColorAttributeName : [UIColor blackColor],NSParagraphStyleAttributeName : paragraph}];
    
    CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedString);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, CGRectMake(0, 0, self.frame.size.width, MAXFLOAT));
    NSInteger length = attributedString.length;
    CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, length), path, NULL);
    
    NSString *lineString = [self keyLineForKeyword:keyword frame:frame text:self.text];
    CFRelease(frame);
    CFRelease(path);
    CFRelease(frameSetter);
    
    UIColor *color = [UIColor colorWithRed:((0x0076DA>>16)&0xFF)/255.0 green:((0x0076DA>>8)&0xFF)/255.0 blue:(0x0076DA&0xFF)/255.0 alpha:1.0];
    
    NSRange keywordRange = [lineString rangeOfString:keyword];

    NSMutableAttributedString *ret = [[NSMutableAttributedString alloc] initWithString:lineString attributes:@{NSFontAttributeName:self.font, NSForegroundColorAttributeName:self.textColor}];
    [ret addAttribute:NSForegroundColorAttributeName value:color range:keywordRange];
    return ret;
}

- (NSString *)keyLineForKeyword:(NSString *)keyword frame:(CTFrameRef)frame text:(NSString *)text
{
    NSString *lineString;
    NSArray * arrLines = (NSArray *)CTFrameGetLines(frame);
    NSInteger count = [arrLines count];
    CGPoint points[count];
    CTFrameGetLineOrigins(frame, CFRangeMake(0, 0), points);
//    static NSString *ellipsis = @"...";
    if (count > 1) {
        for (int i = 0; i < count; i ++) {
            CTLineRef line = (__bridge CTLineRef)arrLines[i];
            CFRange cfRange = CTLineGetStringRange(line);
            NSRange tmpRange = NSMakeRange(cfRange.location, cfRange.length);
            NSString *string = [text substringWithRange:tmpRange];
            if ([string containsString:keyword]) {
//                BOOL firstLine = i==0 ? true : false;
//                BOOL lastLine = (i+1)==count ? true : false;
//                NSString *beginWord;
//                NSRange keywordRange = [string rangeOfString:keyword];
//                if (firstLine == false) {
//                    // 不是在前三位
//                    if (string.length > ellipsis.length && keywordRange.location > ellipsis.length) {
//                        beginWord = [string substringWithRange:NSMakeRange(ellipsis.length-1, string.length-ellipsis.length)];
//                        string = [NSString stringWithFormat:@"%@%@",ellipsis,beginWord];
//                    }
//                }
//                if (lastLine==false) {
//                    // 不是在后三位
//                    if (string.length > ellipsis.length) {
//                        if ((keywordRange.location + keyword.length) < (string.length-keywordRange.length)) {
//                            beginWord = [string substringWithRange:NSMakeRange(0, string.length-ellipsis.length)];
//                            string = [NSString stringWithFormat:@"%@%@",beginWord,ellipsis];
//                        }
//                    }
//                }
                lineString = string;
                break;
            }
        }
    }else{
        lineString = text;
    }
    
    return lineString;
}

你可能感兴趣的:(微信消息搜索 只显示关键字)