正则表达式在内容里面标示 @昵称 #话题# url

效果图:



/*

注意事项:

1.使用之前需要倒入 libicucore.dylib And CoreText.framework

2.此类使用了ARC管理内存

3.如果你的项目是非ARC项目,你需要在文件添加-fobjc-arc的标示(非ARC标示-fno-objc-arc

*/


使用方法

        //7.文本
        WXLabel *text = [[WXLabel alloc] init];
        [self.topView addSubview:text];
        text.font = textFont;
        text.numberOfLines = 0;
        [text sizetoFit];
//        text.colorName = @"Theme_Main_color";
        self.text = text;
        self.text.wxLabelDelegate = self;

#pragma mark - WXLabelDelegate

//(1)检索文本的正则表达式的字符串
- (Nsstring *)contentsOFregexStringWithWXLabel:(WXLabel *)wxLabel {
    
    //@某人   #话题#   http(s)://......
    
    //@某人
    Nsstring *people = @"@\\w+";
    
    //#话题#
    Nsstring *topic = @"#[^#]+#";
    
    //网址  http(s)://www.baidu.com/www.baidu.com/www.baidu.com/
    //     Nsstring *regex3 = @"http(s)?://([a-zA-Z0-9._-]+(/)?)*";
    //     Nsstring *regex3 = @"http(s)?://([a-zA-Z0-9._-/?]+)*";
    
    Nsstring *httpStr = @"\\bhttps?://[a-zA-Z0-9\\-.]+(?::(\\d+))?(?:(?:/[a-zA-Z0-9\\-._?,'+\\&%$=~*!():@\\\\]*)+)?";
    
    Nsstring *result = [Nsstring stringWithFormat:@"%@|%@|%@",people,topic,httpStr];
    
    return result;
}

//(2)设置当前超链接的颜色
- (UIColor *)linkColorWithWXLabel:(WXLabel *)wxLabel {
    
    UIColor *color = [[ThemeManager shareInstance] getThemeColor:@"Link_color"];
    
    return color;
}

//(3)设置当前文本手指经过的颜色
- (UIColor *)passColorWithWXLabel:(WXLabel *)wxLabel {
    
    UIColor *color = [UIColor grayColor];
    
    return color;
}

//(4)点击超链接响应的代理方法
//context里面是设置的正则检索字符串,此方法在点击这些字符串时会调用
- (void)toucheEndWXLabel:(WXLabel *)wxLabel withContext:(Nsstring *)context {

    NSLog(@"context:%@",context);
    
}


类库链接: http://pan.baidu.com/s/1nt47LKt 密码: qrei

相关文章

正则替换html代码中img标签的src值在开发富文本信息在移动端...
正则表达式
AWK是一种处理文本文件的语言,是一个强大的文件分析工具。它...
正则表达式是特殊的字符序列,利用事先定义好的特定字符以及...
Python界一名小学生,热心分享编程学习。
收集整理每周优质开发者内容,包括、、等方面。每周五定期发...