美文网首页iOS开发
iOS - label识别text中的网址链接并做链接效果的展示

iOS - label识别text中的网址链接并做链接效果的展示

作者: HanZhiZzzzz | 来源:发表于2018-11-20 14:55 被阅读0次

    识别URL的正则表达式:

    方案一:

    ((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)
    
    

    方案二:

        NSString *regulaStr = @"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)";
    

    可以通过分类的方式给label添加个分类, 为label新增个分类方法:
    .h文件

    #import <UIKit/UIKit.h>
    @interface UILabel (LinkUrl)
    
    - (void)setTextWithLinkAttribute:(NSString *)text;
    
    @end
    

    .m文件

    #import "UILabel+LinkUrl.h"
    
    
    
    @implementation UILabel (LinkUrl)
    
    - (void)setTextWithLinkAttribute:(NSString *)text {
    
        self.attributedText = [self subStr:text];
    
    }
    
    
    -(NSMutableAttributedString*)subStr:(NSString *)string
    
    {
    
        NSError *error;
    
        //可以识别url的正则表达式
    
        NSString *regulaStr = @"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)";
    
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr
    
                                                                               options:NSRegularExpressionCaseInsensitive
    
                                                                                 error:&error];
    
        NSArray *arrayOfAllMatches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
    
        NSMutableArray *arr=[[NSMutableArray alloc]init];
    
        NSMutableArray *rangeArr=[[NSMutableArray alloc]init];
    
    
    
        for (NSTextCheckingResult *match in arrayOfAllMatches)
    
        {
    
            NSString* substringForMatch;
    
            substringForMatch = [string substringWithRange:match.range];
    
            [arr addObject:substringForMatch];
    
    
    
        }
    
        NSString *subStr=string;
    
        for (NSString *str in arr) {
    
            [rangeArr addObject:[self rangesOfString:str inString:subStr]];
    
        }
    
        UIFont *font = [UIFont systemFontOfSize:20];
    
        NSMutableAttributedString *attributedText;
    
        attributedText=[[NSMutableAttributedString alloc]initWithString:subStr attributes:@{NSFontAttributeName :font}];
    
    
    
        for(NSValue *value in rangeArr)
    
        {
    
            NSInteger index=[rangeArr indexOfObject:value];
    
            NSRange range=[value rangeValue];
    
            [attributedText addAttribute:NSLinkAttributeName value:[NSURL URLWithString:[arr objectAtIndex:index]] range:range];
    
            [attributedText addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:range];
    
    
    
        }
    
        return attributedText;
    
    
    
    
    
    }
    
    //获取查找字符串在母串中的NSRange
    
    - (NSValue *)rangesOfString:(NSString *)searchString inString:(NSString *)str {
    
    
    
        NSRange searchRange = NSMakeRange(0, [str length]);
    
    
    
        NSRange range;
    
    
    
        if ((range = [str rangeOfString:searchString options:0 range:searchRange]).location != NSNotFound) {
    
            searchRange = NSMakeRange(NSMaxRange(range), [str length] - NSMaxRange(range));
    
        }
    
        return [NSValue valueWithRange:range];
    
    }
    
    @end
    

    在你创建label的地方调用这个新增方法:

    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(18, 100, CGRectGetWidth(self.view.frame) - 36, 100)];
    
    label.numberOfLines = 0;
    
    [label setTextWithLinkAttribute:@"谷歌搜索好用http://www.google.com搜索用什么https://www.baidu.com百度一下你知道"];
    
    [self.view addSubview:label];
    

    摘自:https://blog.csdn.net/bright_ty/article/details/53671173

    相关文章

      网友评论

        本文标题:iOS - label识别text中的网址链接并做链接效果的展示

        本文链接:https://www.haomeiwen.com/subject/vfcxqqtx.html