YYText识别链接点击效果和输出点击的内容

这篇博客展示了如何利用YYText在iOS中处理富文本链接,包括设置链接样式、捕获点击事件以及获取点击的链接内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

- (void)testUrl{

    // 测试文本

    NSString *text = @"发呆发https://blog.youkuaiyun.com/a18339063397呆发的负担发的分身乏术对方的负担https://www.baidu.com大家看https://www.baidu.com打开房间打开肌肤打开链接附近的空间卡就开个房间卡加客服即可打开附近的卡夫卡的,发呆发https://blog.youkuaiyun.com/a18339063397呆发的负担发的分身乏术对方的负担https://www.baidu.com大家看https://www.baidu.com打开房间打开肌肤打开链接附近的空间卡就开个房间卡加客服即可打开附近的卡夫卡的,发呆发https://blog.youkuaiyun.com/a18339063397呆发的负担发的分身乏术对方的负担https://www.baidu.com大家看https://www.baidu.com打开房间打开肌肤打开链接附近的空间卡就开个房间卡加客服即可打开附近的卡夫卡的。";

    // 转成可变属性字符串

    NSMutableAttributedString * mAttributedString = [NSMutableAttributedString new];

    // 调整行间距段落间距

    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];

    //设置文字两端对齐

    paragraphStyle.alignment = NSTextAlignmentJustified;

    [paragraphStyle setLineSpacing:2];

    [paragraphStyle setParagraphSpacing:4];

    UIFont *font = [UIFont systemFontOfSize:15];

    // 设置文本属性

    NSDictionary *attri = [NSDictionary dictionaryWithObjects:@[font, [UIColor blackColor], paragraphStyle] forKeys:@[NSFontAttributeName, NSForegroundColorAttributeName, NSParagraphStyleAttributeName]];

    [mAttributedString appendAttributedString:[[NSAttributedString alloc] initWithString:text attributes:attri]];

    

    // 匹配条件

    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\\.\\-~!@#$%^&*+?:_/=<>]*)?)";

    

    NSError *error = NULL;

    

    // 根据匹配条件,创建了一个正则表达式

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr

                                                                           options:NSRegularExpressionCaseInsensitive

                                                                             error:&error];

    if (!regex) {

        NSLog(@"正则创建失败error!= %@", [error localizedDescription]);

    } else {

        NSArray *allMatches = [regex matchesInString:mAttributedString.string options:NSMatchingReportCompletion range:NSMakeRange(0, mAttributedString.string.length)];

        for (NSTextCheckingResult *match in allMatches) {

            NSString *substrinsgForMatch2 = [mAttributedString.string substringWithRange:match.range];

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

            // 利用YYText设置一些文本属性

            one.yy_font = font;

            one.yy_underlineStyle = NSUnderlineStyleSingle;

            one.yy_color = [UIColor blueColor];

            

            YYTextBorder *border = [YYTextBorder new];

            border.cornerRadius = 3;

            border.insets = UIEdgeInsetsMake(-2, -1, -2, -1);

            //设定颜色则点击的时候,点击链接的时候链接颜色不会变

           // border.fillColor = [UIColor greenColor];

            

            YYTextHighlight *highlight = [YYTextHighlight new];

            [highlight setBorder:border];

            [highlight setColor:[UIColor redColor]];

            [one yy_setTextHighlight:highlight range:one.yy_rangeOfAll];

            // 根据range替换字符串

            [mAttributedString replaceCharactersInRange:match.range withAttributedString:one];

        }

    }

    // 使用YYLabel显示

    YYLabel *label = [YYLabel new];

    label.userInteractionEnabled = YES;

    label.backgroundColor = [UIColor cyanColor];

    label.numberOfLines = 0;

    label.textVerticalAlignment = YYTextVerticalAlignmentTop;

//    label.size = CGSizeMake(Screen_Width, 260);

//    label.center = CGPointMake(self.view.width / 2 , 250);

    //具体高度有内容决定,不会按照这里设定的

    label.frame = CGRectMake(0, 100, Screen_Width, 100);

    

    

    label.attributedText = mAttributedString;

    [self.view addSubview:label];

    label.highlightTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {

        NSString *string = [NSString stringWithFormat:@"Tap: %@",[text.string substringWithRange:range]];

        NSLog(@"%@", string);

    };

    // 利用YYTextLayout计算高度

    YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(Screen_Width, MAXFLOAT)];

    YYTextLayout *textLayout = [YYTextLayout layoutWithContainer:container text: mAttributedString];

    label.height = textLayout.textBoundingSize.height;

    

    NSLog(@"%f",label.height);

    

    

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值