1.創(chuàng)建UITextView
UITextView *_cqAirlinesAgreement = [UITextView new];
_cqAirlinesAgreement.attributedText = [self getContentLabelAttributedText:_@"xxxxxxxx《點(diǎn)擊進(jìn)入H5》xxxxxx"];
_cqAirlinesAgreement.textAlignment = NSTextAlignmentLeft;
_cqAirlinesAgreement.delegate = self;
_cqAirlinesAgreement.editable = NO; //必須禁止輸入,否則點(diǎn)擊將彈出輸入鍵盤
_cqAirlinesAgreement.scrollEnabled = NO;
2.創(chuàng)建getContentLabelAttributedText
- (NSAttributedString *)getContentLabelAttributedText:(NSString *)text
{
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13],NSForegroundColorAttributeName:kHexColor(@"#333333")}];
NSRange range1 = [[attrStr string] rangeOfString:@"《點(diǎn)擊進(jìn)入H5》"];
[attrStr addAttribute:NSForegroundColorAttributeName value:kHexColor(@"#3198FF") range:range1];
[attrStr addAttribute:NSLinkAttributeName value:@"changeRules://" range:range1];
return attrStr;
}
3.監(jiān)聽點(diǎn)擊
#pragma mark - UITextViewDelegate ----核心代碼
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
if ([[URL scheme] isEqualToString:@"changeRules"]) {
//監(jiān)聽
return NO;
}
return YES;
}