微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

正则表达式在内容里面标示 @昵称 #话题# 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

原文地址:https://www.jb51.cc/regex/360123.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐