iOS 通过TextView富文本给文字添加点击事件

UITextView *protocolTV = [[UITextView alloc]initWithFrame:CGRectMake(50,200, 200, 100)];

//    protocolTV.frame = CGRectMake(15, 15, SCREEN_WIDTH - 30, 40);

    protocolTV.editable = NO;

    protocolTV.delegate = self;

    protocolTV.textContainer.lineFragmentPadding = 0.0;

    protocolTV.textContainerInset = UIEdgeInsetsMake(15, 0, 0,  0);

    protocolTV.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor blueColor]};

    [self.view addSubview:protocolTV];

    NSString *appName = [NSBundle mainBundle].infoDictionary[@"CFBundleDisplayName"];

    NSString *rangeStr = [NSString stringWithFormat:@"这是我们要遵循的《%@隐私政策》",appName];

    NSString *protocolStr = [NSString stringWithFormat:@"阅读并同意%@",rangeStr];

    NSString *serverce= [NSString stringWithFormat:@"《服务协议》"];

    NSRange privacyRange = [protocolStr rangeOfString:rangeStr];

    NSString *preStr = [NSString stringWithFormat:@"这是我们要遵循的"];

    NSMutableAttributedString *privacy = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@%@",preStr,protocolStr] attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor blueColor]}];

    NSRange range = [rangeStr rangeOfString:preStr];

    [privacy setAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:20]} range:[rangeStr rangeOfString:preStr]];

    [privacy addAttribute:NSLinkAttributeName value:@"privacy://" range:privacyRange];

   

   

    NSMutableAttributedString *servercePrivacy = [[NSMutableAttributedString alloc]initWithString:serverce attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20.0],NSForegroundColorAttributeName:[UIColor blueColor]}];

    NSRange serverceRange = [serverce rangeOfString:serverce];

    [servercePrivacy addAttribute:NSLinkAttributeName value:@"serverce://" range:serverceRange];

    [privacy appendAttributedString:servercePrivacy];

    protocolTV.attributedText = privacy;

    [self.view addSubview:protocolTV];

 

 

-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(nonnull NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction{

    if ([URL.scheme isEqualToString:@"privacy"]) {

        NSLog(@"点击了隐私协议");

    }else if ([URL.scheme isEqualToString:@"serverce"]){

        NSLog(@"点击了服务协议");

    }

    return YES;

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
安卓中的TextView控件默认是不支持富文本的,即无法直接实现文字点击的效果。不过我们可以通过一些方法来实现这个需求。 一种常用的方法是使用SpannableString类来实现富文本文字点击的效果。SpannableString是一个可以调整文字的样式和属性的类。我们可以使用它的setSpan()方法来设置文字点击事件。 首先,我们需要创建一个ClickableSpan对象,它是一个可以实现文字点击的类。在ClickableSpan的onClick()方法中,我们可以编写点击文字后的逻辑代码。 ``` ClickableSpan clickableSpan = new ClickableSpan() { @Override public void onClick(View widget) { // 在这里编写点击文字后的逻辑代码 } }; ``` 然后,我们创建一个SpannableString对象,并使用setSpan()方法将ClickableSpan对象应用于需要点击文字范围。 ``` SpannableString spannableString = new SpannableString("需要设置点击事件文字"); spannableString.setSpan(clickableSpan, startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ``` 其中,startIndex和endIndex分别表示需要设置点击事件文字的起始和结束位置。 接下来,我们将SpannableString对象设置给TextView控件,并为TextView控件设置setMovementMethod()方法,使其具有点击效果。 ``` textView.setText(spannableString); textView.setMovementMethod(LinkMovementMethod.getInstance()); ``` 最后,我们就可以在TextView中实现文字点击的效果了。 需要注意的是,在使用这种方法时,TextView的同时三个属性要设置为true:android:focusable="true"、android:focusableInTouchMode="true"和android:clickable="true",以确保TextView本身可以获得焦点和点击事件。 以上就是使用SpannableString实现安卓TextView富文本文字点击的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值