1.创建UITextView
UITextView *_cqAirlinesAgreement = [UITextView new];
_cqAirlinesAgreement.attributedText = [self getContentLabelAttributedText:_@"xxxxxxxx《点击进入H5》xxxxxx"];
_cqAirlinesAgreement.textAlignment = NSTextAlignmentLeft;
_cqAirlinesAgreement.delegate = self;
_cqAirlinesAgreement.editable = NO; //必须禁止输入,否则点击将弹出输入键盘
_cqAirlinesAgreement.scrollEnabled = NO;
2.创建getContentLabelAttributedText
- (NSAttributedString *)getContentLabelAttributedText:(NSString *)text
{
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13],NSForegroundColorAttributeName:kHexColor(@"#333333")}];
NSRange range1 = [[attrStr string] rangeOfString:@"《点击进入H5》"];
[attrStr addAttribute:NSForegroundColorAttributeName value:kHexColor(@"#3198FF") range:range1];
[attrStr addAttribute:NSLinkAttributeName value:@"changeRules://" range:range1];
return attrStr;
}
3.监听点击
#pragma mark - UITextViewDelegate ----核心代码
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
if ([[URL scheme] isEqualToString:@"changeRules"]) {
//监听
return NO;
}
return YES;
}