iOS 文本点击事件-爱代码爱编程
项目中总会遇到给文本添加点击事件的问题,这里我使用的是textView控件.
@property (strong, nonatomic) IBOutlet UITextView *textView;
- (void)initTextView {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"尊敬的用户,我们尊重并保护用户隐私,为了更好地保障您的个人权益,在使用我们的服务前,请您仔细阅读《用户使用协议》及《隐私政策》各项条款,我们会按照上述条款收集、使用和储存您的个人信息。"];
[attributedString addAttribute:NSLinkAttributeName
value:@"yonghu://"
range:[[attributedString string] rangeOfString:@"《用户使用协议》"]];
[attributedString addAttribute:NSLinkAttributeName
value:@"yinsi://"
range:[[attributedString string] rangeOfString:@"《隐私政策》"]];
self.textView.attributedText = attributedString;
self.textView.linkTextAttributes = @{NSForegroundColorAttributeName: HEXCOLOR(0x4CA668),
NSUnderlineColorAttributeName: HEXCOLOR(0x232323),
NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid),
NSFontAttributeName:[UIFont systemFontOfSize:13]
};
self.textView.delegate = self;
self.textView.editable = NO;
self.textView.scrollEnabled = NO;
}
//实现textView的代理方法
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
if ([[URL scheme] isEqualToString:@"yonghu"]) {
//在这里实现要处理的操作
return NO;
} else if ([[URL scheme] isEqualToString:@"yinsi"]) {
//在这里实现要处理的操作
return NO;
}
return YES;
}
欢迎大家指正,如有问题,请联系我们
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接: https://blog.csdn.net/weixin_48567147/article/details/111056655