iOS开发--UILabel加载Html内容, 并添加超链接的点击

场景:

项目中的简介(关于)页面, 服务器给返回了Html的语句, 展示相关的内容

方案:

  1. 想用WKWebView去加载, 但是, WK的在轻量级需求而言, 消耗资源大就成了其短板.
  2. 使用UILabel + 富文本

对比两个方案, 开启WK的消耗资源大, 再加上当前页面只是文字展示, 所以, 方案二是相对而言的最佳方案.

解决:

在解决的过程中, 本来以为只是展示出就可以了, 发现Html中包含了href的超链接, 所以, 如果你的项目中, 也有包含href的超链接, 那么可以参考步骤2和步骤3, 如果没有只看步骤1即可

1. 将html转换成富文本

重点为: NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType

- (NSMutableAttributedString *)getContentHtml:(NSString *)content {
    NSDictionary *options = @{NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,
                              NSCharacterEncodingDocumentAttribute : @(NSUTF8StringEncoding)};
    NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithData:data options:options documentAttributes:nil error:nil];
    
    // 设置段落格式
    NSMutableParagraphStyle *para = [[NSMutableParagraphStyle alloc] init];
    para.lineSpacing = 8;
	para.paragraphSpacing = 10;
    [attStr addAttribute:NSParagraphStyleAttributeName value:para range:NSMakeRange(0, attStr.length)];
    
    // 设置文本的Font
    [attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, attStr.length)];
    
    return attStr;
}
2. 给UILabel添加点击事件, 注意, 这里需要将UILabel的用户交互打开
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickLink)];
// 打开用户交互
self.contentLabel.userInteractionEnabled = YES;
[self.contentLabel addGestureRecognizer:tap];
3. 处理点击UILabel点击富文本中的超链接

解析html中的链接 NSTextCheckingTypeLink

- (void)clickLink {
    NSString *chatStr = @"这里需要填写Html内容, 不是UILabel的text 或者 NSMutableAttributedString";
    
    NSDataDetector *detector= [[NSDataDetector alloc] initWithTypes:NSTextCheckingTypeLink error:nil];
    NSArray *checkArr = [detector matchesInString:chatStr options:0 range:NSMakeRange(0, chatStr.length)];
    //判断有没有链接
    if(checkArr.count > 0) {
        
        LTCWebViewController *webVC =  [[LTCWebViewController alloc] init];
        if (checkArr.count > 1) { //网址多于1个时让用户选择跳哪个链接
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请选择要打开的链接" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
            [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
            
            for (NSTextCheckingResult *result in checkArr) {
                NSString *urlStr = result.URL.absoluteString;
                [alertController addAction:[UIAlertAction actionWithTitle:urlStr style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                    webVC.urlString = urlStr;
                    [self.navigationController pushViewController:webVC animated:YES];
                }]];
            }
            [self presentViewController:alertController animated:YES completion:nil];
            
        }else {//一个链接直接打开
            
            webVC.urlString = [checkArr[0] URL].absoluteString;
            [self.navigationController pushViewController:webVC animated:YES];
        }
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值