iOS 正则表达(一) 标签的处理

首先你需要第三方RegexKitLite以及YYKit

1. 我们先来说说RegexKitLite的导入

你可以在github上下载并导入你的项目,也可以用cocopods导入。

  • 使用cocopods


    iOS 正则表达(一) <a>标签的处理_第2张图片
    这样相对来说比较简单,不用管其他配置了
  • 从github下载 RegexKitLite导入你的项目中,然后你需要配置以下:
    iOS 正则表达(一) <a>标签的处理_第3张图片
    Build Phases需要配置

2. 至于YYKit,同样你可以选择手动导入或者cocopods第三方管理工具
  • 使用cocopods
  1. 在podfile中添加 pod ‘YYKit’
  2. 执行pod install
  • 手动安装
  1. 下载YYKit并将YYKit中源文件拖放如你的工程
  • 为 NSObject+YYAddForARC.m 和 NSThread+YYAdd.m 添加编译参数 -fno-objc-arc
  • 链接以下 frameworks:
  • UIKit
  • CoreFoundation
  • CoreText
  • CoreGraphics
  • CoreImage
  • QuartzCore
  • ImageIO
  • AssetsLibrary
  • Accelerate
  • MobileCoreServices
  • SystemConfiguration
  • sqlite3
  • libz

接下来就是我们需要做的就是代码搞起来

1. 导入头文件
#import "RegexKitLite.h"
#import "YYKit.h"
2. 主要代码
NSString *regex_http = @"(.*?)<\\/a>";
    NSString *labelText = @"快件查询页面";
    NSArray *array_http = [labelText arrayOfCaptureComponentsMatchedByRegex:regex_http];
    
    // 高亮状态的背景
    YYTextBorder *highlightBorder = [YYTextBorder new];
    highlightBorder.insets = UIEdgeInsetsMake(-2, 0, -2, 0);
    highlightBorder.cornerRadius = 3;
    highlightBorder.fillColor = [UIColor lightGrayColor];
    
    if ([array_http count]) {
        // 先把html a标签都给去掉
        labelText = [labelText stringByReplacingOccurrencesOfString:@"" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange (0, labelText.length)];
        labelText = [labelText stringByReplacingOccurrencesOfString:@"<\\/a>" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange (0, labelText.length)];
        NSLog(@"labelText === %@", labelText);
        
        NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc] initWithString:labelText];
        aStr.font = [UIFont systemFontOfSize:14];
        aStr.color = [UIColor blackColor];
        for (NSArray *array in array_http) {
            // 获得链接显示文字的range,用来设置下划线
            NSRange range = [labelText rangeOfString:array[1]];
            [aStr setColor:[UIColor blueColor] range:range];
            
            // 高亮状态
            YYTextHighlight *highlight = [YYTextHighlight new];
            [highlight setBackgroundBorder:highlightBorder];
            // 数据信息,用于稍后用户点击
            NSString *linkStr = [array.firstObject componentsSeparatedByString:@"\""][1];
            highlight.userInfo = @{@"linkUrl": linkStr};
            [aStr setTextHighlight:highlight range:range];
        }
        
        _herfLabel.attributedText = aStr;
    }
    
    _herfLabel.highlightTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect){
        text = [text attributedSubstringFromRange:range];
        NSDictionary *dic = text.attributes;
        YYTextHighlight *user = [dic valueForKey:@"YYTextHighlight"];
        NSString *linkText = [user.userInfo valueForKey:@"linkUrl"];
        NSLog(@"获取的linkText:%@  在这里可以跳转界面", linkText);
    };

提示:_herfLabel是YYLabel,不是普通的UILabel

最后,下次有时间会附上demo。

你可能感兴趣的:(iOS 正则表达(一) 标签的处理)