iOS UITextView 文字添加点击事件

- (UITextView *)textView{
   if (!_textView) {
       _textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 300, 200)];
       _textView.backgroundColor = [UIColor whiteColor];
       _textView.editable = false;
       _textView.scrollEnabled = false;
       _textView.delegate = self;
   }
   return _textView;
}

关键代码

NSString *htmlString = [NSString stringWithFormat:@"在信息科技时代,秉持着“以创新科技立业,为客户创造价值,塑造品牌传播力”的理念,对外输出先进的移动端信息技术;如有疑问您可以访问https://www.baidu.com"];
                                                                                       
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:htmlString];
[attributedString addAttributes:@{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),NSLinkAttributeName:@""} range:NSMakeRange(5, 4)];
[attributedString addAttributes:@{NSLinkAttributeName:@""} range:NSMakeRange(27, 4)];
[attributedString addAttributes:@{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),NSLinkAttributeName:@"https://www.baidu.com"} range:NSMakeRange(62, 21)];
NSMutableParagraphStyle *style = [attributedString attribute:NSParagraphStyleAttributeName atIndex:0 effectiveRange:nil];
style.alignment = NSTextAlignmentLeft;
self.textView.attributedText = attributedString;
self.textView.font = [UIFont systemFontOfSize:17];

代理处理事件

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:    (NSRange)characterRange interaction:(UITextItemInteraction)interaction{
   NSLog(@"url:%@, %@", URL, NSStringFromRange(characterRange));

   return true;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容