可以编辑 TextView 的 TextView 链接 (Swift 4)
Links to TextView with the possibility of editing TextView (Swift 4)
我有一个 TextView。
我怎样才能让 link 工作 而你可以 编辑 TextView?
我怎样才能做到,当我 post 一个 link 到 TextView 时,就可以单击它并越过它(并用不同的颜色突出显示它)。
我尝试在设置中包含 Link,但无法编辑 TextView。
为了能够从您的 textView
中检测到 link,您需要将 editable
设置为 false
。您可以做的是向 textView 添加一个手势识别器来检测点击,该手势识别器将忽略 links 上的点击。下面的示例(阅读注释以获取解释):
class ViewController: UIViewController, UITextViewDelegate {
// create an outlet for your textView
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
// add a tap gesture to your textView
let tap = UITapGestureRecognizer(target: self, action: #selector(textViewTapped))
textView.addGestureRecognizer(tap)
//add a tap recognizer to stop editing when you tap outside your textView (if you want to)
let viewTap = UITapGestureRecognizer(target: self, action: #selector(viewTapped))
self.view.addGestureRecognizer(viewTap)
}
@objc func viewTapped(_ aRecognizer: UITapGestureRecognizer) {
self.view.endEditing(true)
}
// when you tap on your textView you set the property isEditable to true and you´ll be able to edit the text. If you click on a link you´ll browse to that link instead
@objc func textViewTapped(_ aRecognizer: UITapGestureRecognizer) {
textView.dataDetectorTypes = []
textView.isEditable = true
textView.becomeFirstResponder()
}
// this delegate method restes the isEditable property when your done editing
func textViewDidEndEditing(_ textView: UITextView) {
textView.isEditable = false
textView.dataDetectorTypes = .all
}
}
textView 具有以下属性:
并且 here 是我创建的示例项目的 link。
我有一个 TextView。
我怎样才能让 link 工作 而你可以 编辑 TextView?
我怎样才能做到,当我 post 一个 link 到 TextView 时,就可以单击它并越过它(并用不同的颜色突出显示它)。
我尝试在设置中包含 Link,但无法编辑 TextView。
为了能够从您的 textView
中检测到 link,您需要将 editable
设置为 false
。您可以做的是向 textView 添加一个手势识别器来检测点击,该手势识别器将忽略 links 上的点击。下面的示例(阅读注释以获取解释):
class ViewController: UIViewController, UITextViewDelegate {
// create an outlet for your textView
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
// add a tap gesture to your textView
let tap = UITapGestureRecognizer(target: self, action: #selector(textViewTapped))
textView.addGestureRecognizer(tap)
//add a tap recognizer to stop editing when you tap outside your textView (if you want to)
let viewTap = UITapGestureRecognizer(target: self, action: #selector(viewTapped))
self.view.addGestureRecognizer(viewTap)
}
@objc func viewTapped(_ aRecognizer: UITapGestureRecognizer) {
self.view.endEditing(true)
}
// when you tap on your textView you set the property isEditable to true and you´ll be able to edit the text. If you click on a link you´ll browse to that link instead
@objc func textViewTapped(_ aRecognizer: UITapGestureRecognizer) {
textView.dataDetectorTypes = []
textView.isEditable = true
textView.becomeFirstResponder()
}
// this delegate method restes the isEditable property when your done editing
func textViewDidEndEditing(_ textView: UITextView) {
textView.isEditable = false
textView.dataDetectorTypes = .all
}
}
textView 具有以下属性:
并且 here 是我创建的示例项目的 link。