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我们深知个人隐私的重要性,在您使用我们的产品和服务时所提供的个人信息将只用于本《隐私权政策》中规定的用途,nn如您同意";
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);
}

效果如下

人已赞赏
iOS文章

iOS 归档和反归档

2020-3-6 16:16:19

iOS文章

iOS截图

2020-3-6 16:59:41

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索