gpt4 book ai didi

swift - 链接到 TextView 并可以编辑 TextView (Swift 4)

转载 作者:搜寻专家 更新时间:2023-10-30 23:08:01 25 4
gpt4 key购买 nike

我有一个 TextView。

我怎样才能使链接有效并且您可以编辑TextView

我怎样才能做到这一点,当我发布一个指向 TextView 的链接时,就可以点击它并浏览它(并用不同的颜色突出显示它)。

我尝试在设置中包含链接,但无法编辑 TextView。

enter image description here

最佳答案

为了能够检测来自 textView 的链接,您需要将 editable 设置为 false。您可以做的是向您的 textView 添加一个手势识别器来检测点击,并且该手势识别器将忽略链接上的点击。下面的示例(阅读注释以获取解释):

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 具有以下属性: enter image description here

here是指向我创建的示例项目的链接。

关于swift - 链接到 TextView 并可以编辑 TextView (Swift 4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47737781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com