ios 富文本 实现UITextView中关注字可点击可点击

//添加隐私权政策提示view
-(void)addPrivacyAlartView{
    bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    bgview.backgroundColor = [UIColor lightGrayColor];
    bgview.alpha = 0.3;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:bgview];
    
    NSString *str1 = @"您好,感谢您对XXAPP的信任。\n在使用app前,在使用APP前,请仔细阅读《隐私权政策》。了解我们对您个人信息的处理规则及申请权限的目的。\n我们深知个人隐私的重要性,在您使用我们的产品和服务时所提供的个人信息将只用于本《隐私权政策》中规定的用途,\n\n如您同意";
    
    NSString *str2= @" 《隐私权政策》";
    NSString *str3 = @",请点击“同意”开始使用我们的产品和服务》";
    NSString *str = [NSString stringWithFormat:@"%@%@%@",str1,str2,str3];
    float textHeight = calculateRowHeight(str, 15,  ScreenWidth-80)+35;
    
    
    alertView = [[UIView alloc] initWithFrame:CGRectMake(40, 100, ScreenWidth-80, textHeight+25+25+10+40+10+40+30)];
    alertView.backgroundColor = [UIColor whiteColor];
    [window addSubview:alertView];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, CGRectGetWidth(alertView.frame), 20)];
    label.text = @"温馨提示";
    label.textAlignment =NSTextAlignmentCenter;
    [alertView addSubview:label];
    alertView.center = CGPointMake(ScreenWidth/2.0,ScreenHeight/2.0 );
    
    
    UITextView *alTextView = [[UITextView alloc] init];
    alTextView.frame = CGRectMake(10, 25,  CGRectGetWidth(alertView.frame)-20, textHeight);
    alTextView.editable = NO;
    alTextView.delegate = self;

    [alertView addSubview:alTextView];
    alTextView.textAlignment = NSTextAlignmentLeft;
    NSRange range1 = [str rangeOfString:str2];
    NSMutableAttributedString *mastring = [[NSMutableAttributedString alloc] initWithString:str attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0f]}];
    [mastring addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:range1];
    NSString *valueString1 = [[NSString stringWithFormat:@"firstPerson://%@",str2] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    [mastring addAttribute:NSLinkAttributeName value:valueString1 range:range1];
    alTextView.attributedText =mastring;
    
    UIButton *agreeBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [agreeBtn setTitle:@"同意" forState:(UIControlStateNormal)];
    agreeBtn.frame = CGRectMake(10, CGRectGetMaxY(alTextView.frame)+20,  CGRectGetWidth(alertView.frame)-20, 40);
    [agreeBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    agreeBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    agreeBtn.layer.borderWidth = 1;
    [agreeBtn addTarget:self action:@selector(agreeAction) forControlEvents:(UIControlEventTouchUpInside)];
    [alertView addSubview:agreeBtn];
    
    UIButton *unAgreeBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [unAgreeBtn setTitle:@"不同意并退出" forState:(UIControlStateNormal)];
    [unAgreeBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    unAgreeBtn.frame = CGRectMake(10, CGRectGetMaxY(agreeBtn.frame)+20,  CGRectGetWidth(alertView.frame)-20, 40);
    unAgreeBtn.layer.borderWidth = 1;
    unAgreeBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [unAgreeBtn addTarget:self action:@selector(unAgreeAction) forControlEvents:(UIControlEventTouchUpInside)];
    [alertView addSubview:unAgreeBtn];
}

这是UI,对关键字做了颜色,已经类似超链接的处理,下面是关键字点击的触发方法,要用的UITextViewDelegate

//拦截关键字点击,跳转到新的页面
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    if ([URL.scheme isEqualToString:@"firstPerson"]) {
        [bgview removeFromSuperview];
        [alertView removeFromSuperview];
        navWebViewController * vc = [[navWebViewController alloc] init];
        vc.webUrl = self.homePolicyUrl;
        vc.hidesBottomBarWhenPushed = YES;
        vc.goback = ^(int statusCode) {
            [self addPrivacyAlartView];
        };
        vc.title = @"隐私权政策";
        [textView removeFromSuperview];
        [self.navigationController pushViewController:vc animated:NO];
        return NO;
    }
    return YES;
    
}
//同意
-(void)agreeAction{
    [bgview removeFromSuperview];
    [alertView removeFromSuperview];
 
}
//不同意,强制退出
-(void)unAgreeAction{
    exit(0);
}

 

效果如下 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值