2017-12-10 16 views
1

私はTextViewを持っています。編集の可能性を持つTextViewへのリンクTextView(Swift 4)

どのように私はそれを行うことができますリンクはを仕事とあなたは編集TextViewには可能性の両方のように?

TextViewへのリンクを投稿すると、そのリンクをクリックして上に移動して(別の色で強調表示する)ことができます。

リンクに設定を含めようとしましたが、TextViewを編集できませんでした。

enter image description here

+0

"Editable" = true ... –

+0

@AndreaMugnainiその後、リンクが機能しない – B2Fq

+0

@AndreaMugnaini、もし彼がクリックすれば、リンクはクリックできなくなります。この問題を解決する方法の下で私の答えで私の説明をチェックアウトする。 –

答えて

3

からのリンクを検出することができるようにするには、あなたのtextViewあなたはfalseeditableセットを持っている必要があります。あなたができることは、クリックを検出するためにあなたの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は、私が作成したサンプルプロジェクトへのリンクです。

+0

ありがとう巨大なすべての作品 – B2Fq

+0

NpはB2Fqを助けてうれしい –

関連する問題