label标记关键字

来源:互联网 发布:钻石展位 淘宝首页 编辑:程序博客网 时间:2024/06/18 18:41

可以标记多组关键字 以数组方式存入

直接上代码  喜欢点赞 拿走不谢!



#import "lab_Search.h"


@implementation lab_Search


/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code

}

*/

- (NSArray *)IsChinese:(NSString *)str

{

    NSArray *chineses;

    NSMutableArray *mChineses = [NSMutableArrayarray];

    

    for (int i =0; i < [str length]; ++i)

    {

        int a = [strcharacterAtIndex:i];

        

        if (a >0x4e00 && a < 0x9fff)

        {

            [mChineses addObject:[strsubstringWithRange:(NSRange){i,1}]];

        }

    }

    

    chineses = mChineses;

    return chineses;

}

#pragma  mark -- 设置关键字


#pragma  mark -- 设置关键字

-(void)attributeStringWithContent:(NSString *)content keyWords:(NSArray *)keyWords colors:(NSArray *)colors repeat:(BOOL)repeat

{

    

    NSMutableAttributedString *attString = [[NSMutableAttributedStringalloc] initWithString:content];

    if (keyWords) {

        

        [keyWords enumerateObjectsUsingBlock:^(id obj,NSUInteger idx, BOOL *stop) {

            NSMutableString *tmpString=[NSMutableStringstringWithString:content];

            NSRange range=[contentrangeOfString:obj];

            NSInteger location=0;

            while (range.length>0) {

                UIColor *color=nil;

                if (!colors[0]) {

                    color=[UIColorcolorWithHexString:@"#00c2c9"];

                }else{

                    color=colors[0];

                }

                [attString addAttribute:(NSString*)NSForegroundColorAttributeNamevalue:color range:NSMakeRange(location+range.location, range.length)];

                location+=(range.location+range.length);

                NSString *tmp= [tmpStringsubstringWithRange:NSMakeRange(range.location+range.length, content.length-location)];

                tmpString=[NSMutableStringstringWithString:tmp];

                range=[tmp rangeOfString:obj];

                if (!repeat) {

                    break;

                }

            }

            

        }];

    }

    self.attributedText = attString;

    

}

@end


1 0
原创粉丝点击