iOS textView的富文本点击事件

 NSString *content = @"xxxxxxxxx";
    _textView.attributedText = [self getContentLabelAttributedText:content];
    _textView.textAlignment = NSTextAlignmentLeft;
    _textView.delegate = self;
    _textView.editable = NO;        //必须禁止输入,否则点击将弹出输入键盘
    _textView.scrollEnabled = NO;


#pragma mark - UITextViewDelegate ----核心代码
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    if ([[URL scheme] isEqualToString:@"privacyPolicy"]) {
        //《隐私政策》
        NSLog(@"点击了《隐私政策》");
        [JumpUtil presentUserAgreement:self type:AgreementPrivacy];
        return NO;
    }else if ([[URL scheme] isEqualToString:@"userProtocol"]) {
        //《用户协议》
        NSLog(@"点击了《用户协议》");
        [JumpUtil presentUserAgreement:self type:AgreementUser];
        return NO;
    }
    return YES;
}

#pragma Others
//----------------------------------
- (NSAttributedString *)getContentLabelAttributedText:(NSString *)text
{
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13],NSForegroundColorAttributeName:HexRGB(0x333333)}];
    
    NSRange range1 = [[attrStr string] rangeOfString:@"《用户协议》"];
    [attrStr addAttribute:NSForegroundColorAttributeName value:HexRGB(0x528DF0) range:range1];
    [attrStr addAttribute:NSLinkAttributeName value:@"userProtocol://" range:range1];
    
    NSRange range2 = [[attrStr string] rangeOfString:@"《隐私协议》"];
    [attrStr addAttribute:NSForegroundColorAttributeName value:HexRGB(0x528DF0) range:range2];
    [attrStr addAttribute:NSLinkAttributeName value:@"privacyPolicy://" range:range2];

    return attrStr;
}

 

你可能感兴趣的:(ios开发)