iostext添加点击事件_iOS textView的富文本点击事件

此博客展示了在iOS中为UITextView添加点击事件的代码。通过设置属性禁止输入和滚动,实现点击《隐私政策》和《用户协议》的响应逻辑。还给出了为文本添加属性和链接的方法,以实现特定文本的点击交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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、付费专栏及课程。

余额充值