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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值