2017-12-10 10 views
1

TextView가 있습니다.편집 가능성 TextView (Swift 4)

는 어떻게 링크 모두 작동하는지 그것을 있도록하고 그럴 수 편집 텍스트 뷰 수 ?

어떻게 TextView에 대한 링크를 게시하면 클릭하여 다른 색상으로 강조 표시 할 수 있습니다.

설정에 Link를 포함하려고 시도했지만 TextView를 편집 할 수 없습니다.

enter image description here

+0

"Editable"= true 일 수 있습니다. –

+0

@AndreaMugnaini 그러면 링크가 작동하지 않습니다. – B2Fq

+0

@AndreaMugnaini, 그가 링크를 클릭하면 클릭 할 수없는 경우. 이 문제를 해결하는 방법 아래 내 대답에 내 설명을 체크 아웃하십시오. –

답변

3

에서 링크를 감지 할 수 있어야하는 textView 당신이 falseeditable 설정이 필요합니다. 할 수있는 일은 클릭을 감지하기 위해 텍스트 인식기에 제스처 인식기를 추가하고 제스처 인식기가 링크의 클릭을 무시한다는 것입니다. 아래의 예 (설명에 대한 의견을 읽고) :

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 
    } 
} 

텍스트 뷰에는 다음과 같은 속성이 있습니다 enter image description here

그리고 here 내가 만든 샘플 프로젝트에 대한 링크입니다.

+0

감사합니다 거대한 모든 작품 – B2Fq

+0

Np 기꺼이 @ B2Fq –