博客> ios改变链接文字颜色
ios改变链接文字颜色
2019-06-27 05:18 评论:0 阅读:738 曾大女神
ios ios改变链接文字颜色 链接文字颜色

//创建一个UITextView

  • (UITextView *)textView { if (!_textView) { _textView = [[UITextView alloc] init]; _textView.editable = NO; _textView.scrollEnabled = NO; _textView.delegate = self; _textView.backgroundColor = [UIColor clearColor]; _textView.attributedText = self.attriStr; _textView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor redColor]};//设置链接的文字颜色 } return _textView; }

//属性字符串

  • (NSMutableAttributedString )attriStr { NSString fullStr = @"是的链接改变颜色,如:查看详情>"; NSMutableAttributedString *attrStr = [NSMutableAttributedString attributedStringWithString:fullStr]; [attrStr addAttribute:NSLinkAttributeName value:@"link" range:[[attrStr string] rangeOfString:@"查看详情>"]]; return attrStr; }

//代理响应想要的action

  • (BOOL)textView:(UITextView )textView shouldInteractWithURL:(NSURL )URL inRange:(NSRange)characterRange { NSString *urlStr = [NSString stringWithFormat:@"%@", URL]; if ([urlStr isEqualToString:@"link"]) { //点击链接后执行你想要的操作 } return YES; }
收藏
1
sina weixin mail 回到顶部