我想让url链接在TextView内可点击,以及使文本可编辑,就像iPhone中的笔记应用程序一样。
这是我尝试过的:
txtVw.dataDetectorTypes = UIDataDetectorTypes.link
txtVw.isSelectable = true
txtVw.isEditable = false. // this is set to false to make link text tappable
txtVw.linkTextAttributes = [.foregroundColor: UIColor.blue, .underlineStyle: NSUnderlineStyle.single.rawValue]
txtVw.isUserInteractionEnabled = true
txtVw.delegate = self
let descTap = UITapGestureRecognizer(target: self, action: #selector(textViewTapped))
descTap.numberOfTapsRequired = 1
txtVw.addGestureRecognizer(descTap)
@objc func textViewTapped(_: UITapGestureRecognizer) {
txtVw.dataDetectorTypes = []
txtVw.isEditable = true
txtVw.becomeFirstResponder()
}这里的问题是,如果我将isEditable属性设置为true,则链接不会格式化,如果不能编辑,则剩余文本。我想要实现和我们在iPhone笔记应用程序中一样的东西。有没有人遇到过这种情况?任何帮助都将不胜感激。
发布于 2021-08-04 07:20:25
首先,您需要使用textView而不是Textfield。它们应用于下面的编程方式来设置颜色或样式。
let attributedString = NSMutableAttributedString(string: "I understand and agree to the Terms of Service.", attributes: [
.font: UIFont.systemFont(ofSize: 12.0, weight: .regular),
.foregroundColor: UIColor.errorColor,
.kern: 0.0
])
attributedString.addAttribute(.link, value: "https://policies.google.com/privacy?hl=en-US", range: NSRange(location: 30, length: 17))
self.termOfServiceTextView.linkTextAttributes = [NSAttributedString.Key.foregroundColor.rawValue: UIColor.errorColor]
attributedString.addAttribute(.underlineStyle, value: 1, range: NSRange(location: 30, length: 17))
self.termOfServiceTextView.attributedText = attributedString现在,为了使其可编辑和可点击,需要从故事板中设置:只需检查Editable,Selectable选项。

https://stackoverflow.com/questions/68646824
复制相似问题