ios html正则表达式,iOS实现html链接a标签正则匹配,高亮

需求:匹配文本内容中的标签,然后高亮显示出来。

运行环境:XCode8.1, iPhone7-iOS10.1

第三方类库框架:

YYText: 富文本渲染类库框架 Github地址

RegexKitLite: 封装了正则匹配操作的方法 Github地址

加入RegexKitLite类库遇到问题:

Undefined symbols for architecture x86_64:

"_u_errorName", referenced from:

_rkl_NSExceptionForRegex in RegexKitLite.o

_rkl_makeNSError in RegexKitLite.o

_rkl_userInfoDictionary in RegexKitLite.o

_rkl_NSExceptionForRegex in MOBFoundation

_rkl_userInfoDictionary in MOBFoundation

"_u_strlen", referenced from:

_rkl_userInfoDictionary in RegexKitLite.o

_rkl_userInfoDictionary in MOBFoundation

"_uregex_appendReplacement", referenced from:

_rkl_replaceAll in RegexKitLite.o

_rkl_replaceAll in MOBFoundation

"_uregex_appendTail", referenced from:

_rkl_replaceAll in RegexKitLite.o

_rkl_replaceAll in MOBFoundation

省略...

解决办法:加入一个flag: -licucore

bVG2tM?w=854&h=592

#import "ViewController.h"

#import "YYText.h"

#import "RegexKitLite.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

YYLabel *label = [[YYLabel alloc] initWithFrame:CGRectMake(0, 100, kScreenWidth, 16)];

label.text = @"占位符占位符#我是链接1#占位符#我是链接2#占占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符";

// url正则有很多种,不过这个已经够满足我的需求

NSString *regex_http = @"(.*?)";

// 文本内容

NSString *labelText = [label.text copy];

//[label.text captureComponentsMatchedByRegex:@""]; // 只会匹配第一个满足条件的

// 这个方法可以匹配多个满足条件的,得到一个二维数组。内容中可能会有多个链接,所以要用这个

NSArray *array_http = [labelText arrayOfCaptureComponentsMatchedByRegex:regex_http];

if ([array_http count]) {

// 先把html a标签都给去掉

labelText = [labelText stringByReplacingOccurrencesOfString:@""

withString:@""

options:NSRegularExpressionSearch

range:NSMakeRange (0, labelText.length)];

labelText = [labelText stringByReplacingOccurrencesOfString:@""

withString:@""

options:NSRegularExpressionSearch

range:NSMakeRange (0, labelText.length)];

// 样式文本

NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString: labelText];

// 处理掉a标签后的内容,用来让UILabel去显示

label.text = labelText;

for (NSArray *array in array_http) {

// 获得链接显示文字的range,用来设置下划线

NSRange range = [labelText rangeOfString:array[1]];

// 设置下划线样式

[one yy_setTextUnderline:[YYTextDecoration decorationWithStyle:YYTextLineStyleSingle] range:range];

// 设置链接文本字体颜色

UIColor *textColor = [UIColor redColor];

[one yy_setColor:textColor range:range];

}

// 设置UILabel样式

label.attributedText = one;

}

[self.view addSubview:label];

}

@end

正则匹配结果图片:

bVG2r7?w=890&h=374

最终效果图片:

bVG2r4?w=459&h=773

注意

, 这个正则这里要主要贪婪匹配的问题。如果是那个会匹配到:

bVG2sL?w=437&h=140

一直匹配到最后一个a标签,就得不到每个链接了。

杂项

YYText也可以实现高亮a链接文本的点击事件。具体可以参考下边的第二篇博客

最后

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值