如何给UITextView 添加连接

本文详细介绍了如何在iOS应用中使用NSAttributedString在UITextView中添加和自定义链接,包括设置链接样式和响应长按事件。

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

要想给UITextView 添加连接 我们要使用到 NSMutableAttributedString 给 UITextView的 attributedText  添加个连接 ,这是 长按会进入到UITextView的 delegate中去  我们只需要在delegate中实现连接就好了 在这里 如果长按的时间稍微长一点调用自己的一个 action 

 具体的实例如下:

               UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 100, 50, 100)];

               textView.delegate = self;

               textView.editable = NO;

               [self.view addSubview:textView];

               NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"This is an example by test"];

              [attributedString addAttribute:NSLinkAttributeName value:@"http://test" range:[[attributedString string] rangeOfString:@"test"]];

    

        NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor],

                                        NSUnderlineColorAttributeName: [UIColor lightGrayColor],

                                        NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)};

             textView.linkTextAttributes = linkAttributes; // customizes the appearance of links

             textView.attributedText = attributedString;

    

         


- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange

{

        // to do some things

    return YES;

}


(单击好像是不可以的)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值