使用UITextView富文本做文字点击 Posted on 2018-10-02 1234567891011121314151617181920212223242526272829303132333435363738// 同意 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;} 效果如图: