How to move cursor of UITextView to TOUCHED LOCATION?

前端 未结 2 978
予麋鹿
予麋鹿 2021-01-01 04:35

The goal of my question is making note app like iOS\'s. The point of iOS\'s note app is that they provide to user to use hyper link or phone number link or email address as

2条回答
  •  猫巷女王i
    2021-01-01 04:48

    Above Answer in Swift 4:

    tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.textViewTapped(recognizer:)))
    textView.addGestureRecognizer(tapGesture)
    
    @objc func textViewTapped(recognizer: UITapGestureRecognizer) {
        if recognizer.state == .ended {     
            textView.isEditable = true
            textView.becomeFirstResponder()
    
            let location = recognizer.location(in: textView)
            if let position = textView.closestPosition(to: location) {
                let uiTextRange = textView.textRange(from: position, to: position)
    
                if let start = uiTextRange?.start, let end = uiTextRange?.end {
                    let loc = textView.offset(from: textView.beginningOfDocument, to: position)
                    let length = textView.offset(from: start, to: end)
    
                    textView.selectedRange = NSMakeRange(loc, length)
    
                }
            }
        }
    }
    

提交回复
热议问题