使用UITextView富文本做文字点击

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// 同意
NSString* str = @"注册表示同意《平台服务协议》与《人人交易隐私策略》";
CGSize size = [str boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size;
UITextView * textView = [[UITextView alloc]initWithFrame:CGRectMake(AppLeftPadding, CGRectGetMaxY(_registerBtn.frame)+12, size.width, 23)];
textView.text = str;
textView.textColor = IMColorGray;
textView.backgroundColor = [UIColor clearColor];
textView.delegate = self;
textView.editable = NO;
textView.scrollEnabled = NO;
[self.view addSubview:textView];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:str];
[attributedString addAttribute:NSLinkAttributeName
value:@"register://"
range:[[attributedString string] rangeOfString:@"《平台服务协议》"]];
[attributedString addAttribute:NSLinkAttributeName
value:@"privacyPolicy://"
range:[[attributedString string] rangeOfString:@"《人人交易隐私策略》"]];
textView.attributedText = attributedString;
textView.linkTextAttributes = @{ NSForegroundColorAttributeName: IMTintBlueColor, NSUnderlineColorAttributeName: [UIColor clearColor], NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};

//==================================================================

#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
if ([[URL scheme] isEqualToString:@"register"]) {
HYBaseWebViewController * web = [[HYBaseWebViewController alloc]init];
web.baseUrl = @"http://106.14.169.21:8080/caopan-h5page/registagree.html";
web.isforceUseoldWebView = NO;
[self.navigationController pushViewController:web animated:YES];
}else if([[URL scheme] isEqualToString:@"privacyPolicy"]){
HYBaseWebViewController * web = [[HYBaseWebViewController alloc]init];
web.baseUrl = [NSString stringWithFormat:@"%@%@", WebH5Address, @"caopan-h5page/privacypolicy.html"];
web.appendUrl = YES;
[self.navigationController pushViewController:web animated:YES];
}
return YES;
}

效果如图: